如何通过XAML为KeyValuePair定义DataTemplate

时间:2011-09-19 06:24:31

标签: wpf xaml dictionary datatemplate key-value

我有一个KeyValuePair。起初我只将Value Part绑定到我的Datatemplate中的TreeView。在这种情况下,绑定无法正常工作,但层次结构模板选择正确。

有没有办法通过KeyValuePair的DataType语法选择正确的Datatemplate?

使用的TreeView:

<TreeView  Margin="5" Grid.RowSpan="2" Grid.Row="0" Name="tvw_mainMenu" Style="    {DynamicResource NavigationTree}" ItemsSource="{Binding Values}"/>

DataContext(代码隐藏):

mainwindow.DataContext = _myObservableDictionary;

使用过的模板                                                      

<HierarchicalDataTemplate DataType="{x:Type local:Scanners}" ItemsSource="{Binding Values}">
    <TextBlock Text="Scanner" FontStyle="Italic"/>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:Scanner}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/scanner.png" />
        <TextBlock Text="{Binding SourceName}" FontStyle="Italic"/>
    </StackPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:FieldInformations}" ItemsSource="{Binding Values}">
    <TextBlock Text="Felder" FontStyle="Italic"/>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:FieldInformation}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/textfield.png" />
        <TextBlock Text="{Binding Caption}" FontStyle="Italic"/>
    </StackPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:Sheets}" ItemsSource="{Binding Values}">
    <TextBlock Text="Seiten" FontStyle="Italic"/>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:Sheet}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/page_green.png" />
        <TextBlock FontStyle="Italic"/>
    </StackPanel>
</HierarchicalDataTemplate>

0 个答案:

没有答案