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
Edit: i forgot to say that i am using c# - wpf. =)
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" >
and the selecteditem was shown as [myNameSpace.Book, 4] instead of only 4.
BookListBox.ItemsSource = LibManager.Books;
this is what I wrote in Window.xaml.cs and Books is a BookList, where BookList is a type of dictionary< Book, int> .
and the xaml file:
< 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>
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…