通过其密钥获取价值,从其他字典到XAML

时间:2012-10-27 10:25:11

标签: wpf xaml

我将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"/>

结果:

enter image description here

看,Employee类有一个Post属性,它是来自ObservableDictionary<Int64, String>(我的某个类)的记录索引。您可以看到'-1'值,但我需要从ObservableDictionary<Int64, String>对象获取此键的值。我可以获得必要的值并通过XAML代码显示它吗?

1 个答案:

答案 0 :(得分:-1)

您可以编写IMultiValueConverter,并将Post属性和字典绑定到它。

请参阅http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx