使用 wpf 在列表框中绑定字典的键和值 [英] Binding a Dictionary's key and value in a listbox with wpf

查看:15
本文介绍了使用 wpf 在列表框中绑定字典的键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字典的键绑定到列表框中网格的一行,并将字典的值绑定到网格的另一行.键的类型是书,我写的一个类,值的类型是 int.我想在网格中写入类的元素和整数值.你能帮我解决这个问题吗?我对确定要绑定的 itemsSource 和数据类型感到很困惑.感谢帮助

I am trying to bind a dictionary's key to a row of the grid in a listbox, and bind the dictionary's value to another row of the grid. key's type is Book, a class thati wrote and the value's type is int. i want to write the elements of the class and the integer value in grid. Can you help me on this? I'm quite confused on determining the itemsSource and the data type to bind. thanks for helping

我忘了说我正在使用 c# - wpf.=)

i forgot to say that i am using c# - wpf. =)

我将字典作为 itemsSource 发送,并将字典指定为 objectdataprovider 标签中的类型,并尝试通过以下代码发送值 (int):

I sent the dictionary as the itemsSource, and i specified the dictionary as type in objectdataprovider tag, and tried to send the value (int) by this code:

< TextBlock Text="{Binding Value, Mode=OneWay}" Grid.Row="1" Width="65" >

并且所选项目显示为 [myNameSpace.Book, 4] 而不是仅 4.

and the selecteditem was shown as [myNameSpace.Book, 4] instead of only 4.

BookListBox.ItemsSource = LibManager.Books;

这是我在Window.xaml.cs中写的,Books是一个BookList,其中BookList是一种字典<书,int> .

this is what I wrote in Window.xaml.cs and Books is a BookList, where BookList is a type of dictionary< Book, int> .

和 xaml 文件:

< ListBox Height="571" HorizontalAlignment="Left" Margin="444,88,0,0"
          Name="BookListBox" VerticalAlignment="Top" Width="383" >
        < ListBox.Resources>
            <ObjectDataProvider x:Key="BookData"
                                ObjectType="{x:Type local:BookList}"/>
        </ListBox.Resources>
        < ListBox.ItemTemplate>
            < DataTemplate>
                < Border BorderThickness="2" BorderBrush="Black" Margin="5"
                         CornerRadius="5" Width="350" >
                    < Grid DataContext="{StaticResource BookData}" >
                        < Grid.ColumnDefinitions>
                            < ColumnDefinition/>                        
                        </Grid.ColumnDefinitions>
                        < Grid.RowDefinitions>
                            < RowDefinition/>
                            < RowDefinition/>
                        < /Grid.RowDefinitions>
                        < Label Content="count: " />
                        < TextBlock Text="{Binding Value, Mode=OneWay}"
                                    Grid.Row="1" Width="65"/>
                    < /Grid>
                < /Border>
            < /DataTemplate>
        < /ListBox.ItemTemplate>
    < /ListBox>

<小时>

我的代码还有一个问题——我在列表框中看不到列出的项目.我的意思是我可以通过 ListBox.SelectedItem 获取值,但在列表框中看不到.因此我无法确定是否可以将整数值传递到我想要的位置.


there is one more problem with my code-- I cant see the listed items in listbox. I mean I could get the values by ListBox.SelectedItem but couldn't see in the listbox. thus I can't be sure if I could pass the integer value to where I want.

所以我想我也需要先解决这个问题...我写了一个自己手写的标签,另一个应该用数据绑定填充的标签在同一行,但我只能看到第一个标签,但我可以到达后面代码中的值.

So I think I also need help for this problem first... I write a label that I write by hand, and another label that should be filled by data binding in the same row but I can just see the first label, but I can reach the value in code behind.

推荐答案

下面的代码将显示以下内容:

Below code will show the following:

1
Book 1
-------
2
Book 2
-------
3
Book 3

SelectedBookIndex 将设置为所选书籍的索引,在开始时将选择第二本书.

SelectedBookIndex will be set to the index of the selected book, at start the second book will be selected.

XAML:

<Window x:Class="TestDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">

    <StackPanel>
        <ListBox 
            ItemsSource="{Binding Path=Books}"
            SelectedValuePath="Value"
            SelectedValue="{Binding Path=SelectedBookIndex}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="2">
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Value}" />
                            <TextBlock Text="{Binding Path=Key.Name}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

背后的代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

namespace TestDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Books = new Dictionary<Book, int>();
            Books.Add(new Book() { Name = "Book 1"}, 1);
            Books.Add(new Book() { Name = "Book 2" }, 2);
            Books.Add(new Book() { Name = "Book 3" }, 3);

            SelectedBookIndex = 2;

            DataContext = this;
        }

        public Dictionary<Book, int> Books { get; set; }

        private int _selectedBookIndex;
        public int SelectedBookIndex
        {
            get { return _selectedBookIndex; }
            set
            {
                _selectedBookIndex = value;
                Debug.WriteLine("Selected Book Index=" + _selectedBookIndex);
            }
        }
    }

    public class Book
    {
        public string Name { get; set; }
    }
}

这篇关于使用 wpf 在列表框中绑定字典的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆