我设置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>
列表框的 XAML:
<ListBox x:Name="lbEmployees" x:FieldModifier="internal"
ItemTemplate="{StaticResource tmpEmploye}" HorizontalContentAlignment="Stretch"/>
结果:
看,这个Employee
类有一个Post
属性,它是来自ObservableDictionary<Int64, String>
(我的某个类)的记录索引。ObservableDictionary<Int64, String>
您可以看到“-1”值,但我需要从我的对象中获取此键的值。我可以获取必要的值并通过 XAML 代码显示它吗?