如何通过 XAML 为 KeyValuePair 定义 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>
I've got a KeyValuePair. At first i've bound only the Value Part to my TreeView in my Datatemplate. In this case, the Binding doesn't work correctly, but the hierachial template is choosen correct.
Is there a way to chose the correct Datatemplate via DataType syntax for a KeyValuePair?
Used TreeView:
<TreeView Margin="5" Grid.RowSpan="2" Grid.Row="0" Name="tvw_mainMenu" Style=" {DynamicResource NavigationTree}" ItemsSource="{Binding Values}"/>
DataContext (Code behind):
mainwindow.DataContext = _myObservableDictionary;
Used Templates:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论