我有一个WPF应用程序,有树视图。这是一个分层的项目模板。
我想使用TreeViewItem将图像源绑定到数据类,即 RestoreItemVM 。我需要在路径中写什么?到目前为止我尝试过的所有内容都在转换器中出现错误,说它无法将其转发到RestoreItemVM ...
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="restoreTab:RestoreItemVM">
<DockPanel VerticalAlignment="Center" HorizontalAlignment="Left" LastChildFill="False">
<CheckBox Focusable="False" VerticalAlignment="Center" IsChecked="{Binding IsChecked}" PreviewMouseRightButtonDown="TreeViewItem_OnPreviewMouseRightButtonDown"/>
<Image Width="20" Margin="3"
Source="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type TreeViewItem},
AncestorLevel=2}, Converter={x:Static local:RestoreItemToImageConverter.Instance},
Path= ????? }"
PreviewMouseRightButtonDown = "TreeViewItem_OnPreviewMouseRightButtonDown"/>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
答案 0 :(得分:1)
您需要指定数据上下文的路径:
function myFunction() {
$(head).append("<style type='text/css'>color:blue</>")
}
但实际上,它更简单,因为RestoreItemVM已经是Image的DataContext,你不需要找到它的祖先。相反,试试这个:
Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}, Converter={x:Static local:RestoreItemToImageConverter.Instance},
Path=DataContext}"
<Image ... Source="{Binding Path=., Converter={x:Static local:RestoreItemToImageConverter.Instance}}" />
绑定到Path=.
本身:
Special symbols in WPF binding - what does "{Binding Path=.}" mean?
DataContext
中DataContext
的{{1}}是DockPanel
中的当前HierarchicalDataTemplate
对象。