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 'So开发者_JAVA技巧urce' (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'
Your XAML uses an XPath in the binding which is only used when binding to an XML document:
{Binding XPath=@Icon}
Try this instead:
{Binding Path=Icon}
There could be something else going on as well.
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:
<StackPanel Orientation="Vertical"
DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem.SourceNode}">
<Image x:Name="CommandIcon"
Width="64"
Height="64"
Source="{Binding XPath=@Icon}"></Image>
</StackPanel>
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}
.
精彩评论