我将ObservableCollection<Employe>
设为ListBox.ItemsSource
的来源,并为DataTemplate
设置了一些ListBox.ItemsSource
。
模板的XAML:
<DataTemplate x:Key="tmpEmploye">
<Border BorderThickness="3" BorderBrush="Gray" CornerRadius="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Surname}"
HorizontalAlignment="Stretch" Margin="2"
FontWeight="Bold" TextWrapping="Wrap"/>
<TextBlock Text="{Binding Path=Name}"
HorizontalAlignment="Stretch" Margin="2"/>
<TextBlock Text="{Binding Path=Patronymic}"
HorizontalAlignment="Stretch" Margin="2"
TextWrapping="Wrap"/>
</StackPanel>
<TextBlock Text="{Binding Path=Post}" Foreground="Gray"
HorizontalAlignment="Stretch" Margin="2"
FontStyle="Italic" TextWrapping="Wrap"/>
</StackPanel>
</Border>
</DataTemplate>
ListBox的XAML:
<ListBox x:Name="lbEmployees" x:FieldModifier="internal"
ItemTemplate="{StaticResource tmpEmploye}" HorizontalContentAlignment="Stretch"/>
结果:
看,Employee
类有一个Post
属性,它是来自ObservableDictionary<Int64, String>
(我的某个类)的记录索引。您可以看到'-1'值,但我需要从ObservableDictionary<Int64, String>
对象获取此键的值。我可以获得必要的值并通过XAML代码显示它吗?
答案 0 :(得分:-1)
您可以编写IMultiValueConverter,并将Post属性和字典绑定到它。
请参阅http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx