绑定到 WPF Treeview 模板项
我有一个 TreeView,其数据上下文是使用
LayoutRoot.DataContext = value
隐藏代码设置的。
CommandTreeViewModel
具有 IEnumerable(Of CommandViewModel)
的 Commands
属性
CommandViewModel
又具有 CommandViewModel 的多个子级
在我的 XAML 中,我使用以下 XAML 将其转换为树项目
<TreeView ItemsSource="{Binding}"
DataContext="{Binding FirstGeneration}"
x:Name="CommandTreeView">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<Border BorderThickness="1"
Width="200"
Margin="2"
CornerRadius="10,0,10,0">
<StackPanel Orientation="Horizontal"
<Image Source="{Binding Icon}"
Width="24"
Height="24" />
<TextBlock VerticalAlignment="Center"
FontSize="13"
Margin="10,0,0,0"
Text="{Binding Name}"
Foreground="White" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
现在我在其他地方有一个图像和两个文本块,我想将它们绑定到所选树视图项目的原始数据源上的元素 - 具体来说, 图标
、描述
、名称
。我尝试绑定它们,如下所示:
<StackPanel Orientation="Vertical"
DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem}">
<Image x:Name="CommandIcon"
Width="64"
Height="64"
Source="{Binding XPath=@Icon}"></Image>
</StackPanel>
与 TextBlocks
的 Text 属性相同。
当我单击树视图项目时,我在输出窗口中遇到以下异常...
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Icon' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'Image' (Name='CommandIcon'); target property is 'Source' (type 'ImageSource') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Name' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandTitle'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Description' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandBody'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'
I've got a TreeView whose data context is set using
LayoutRoot.DataContext = value
from the code-behind.
CommandTreeViewModel
has an Commands
property of IEnumerable(Of CommandViewModel)
CommandViewModel
in turn has multiple children of CommandViewModel
In My XAML, I convert this into tree items using the following XAML
<TreeView ItemsSource="{Binding}"
DataContext="{Binding FirstGeneration}"
x:Name="CommandTreeView">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<Border BorderThickness="1"
Width="200"
Margin="2"
CornerRadius="10,0,10,0">
<StackPanel Orientation="Horizontal"
<Image Source="{Binding Icon}"
Width="24"
Height="24" />
<TextBlock VerticalAlignment="Center"
FontSize="13"
Margin="10,0,0,0"
Text="{Binding Name}"
Foreground="White" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
Now I've got an image and two textblocks elsewhere which I want to bind to elements on the original datasource for the selected treeview item - specifically, Icon
, Description
, Name
. I'm attempting to bind them as shown below:
<StackPanel Orientation="Vertical"
DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem}">
<Image x:Name="CommandIcon"
Width="64"
Height="64"
Source="{Binding XPath=@Icon}"></Image>
</StackPanel>
and the same with the TextBlocks
's Text property.
I'm getting the following exception in the output window when I click on a treeview item...
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Icon' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'Image' (Name='CommandIcon'); target property is 'Source' (type 'ImageSource') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Name' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandTitle'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Description' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandBody'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 XAML 在绑定中使用 XPath,该 XPath 仅在绑定到 XML 文档时使用:
请尝试执行此操作:
还可能发生其他情况。
Your XAML uses an XPath in the binding which is only used when binding to an XML document:
Try this instead:
There could be something else going on as well.
您是否基于某些 XML 构建 CommandViewModel 层次结构?因为如果是这种情况,您的 CommandViewModel 还应该具有一个类似于
XmlNode SourceNode
的属性,该属性链接到相应的 XmlNode,然后在您的 XAML 中您应该这样做:那么它应该可以工作。
或者,如果您根本不使用 XML,请按照 StellarEleven 的建议进行操作 - 从
{Binding XPath=@Icon}
中删除“X”和“@”。Do you build your CommandViewModel hierarchy based on some XML? Because if that is the case your CommandViewModel should also have a property like
XmlNode SourceNode
that links to the corresponding XmlNode and then in your XAML you should do:then it should work.
Or if you don't use XML at all, do as StellarEleven suggested - remove the "X" and "@" from
{Binding XPath=@Icon}
.