У меня есть TreeView, контекст данных которого устанавливается с помощью
LayoutRoot.DataContext = value
из-за кода.
CommandTreeViewModel
имеет Commands
свойство IEnumerable(Of CommandViewModel)
CommandViewModel
в свою очередь имеет несколько детей CommandViewModel
В My 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>
Теперь у меня есть изображение и два текстовых блока в другом месте, которые я хочу привязать к элементам в исходном источнике данных для выбранного элемента древовидной структуры - в частности, Icon
, Description
, Name
. Я пытаюсь связать их, как показано ниже:
<StackPanel Orientation="Vertical"
DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem}">
<Image x:Name="CommandIcon"
Width="64"
Height="64"
Source="{Binding XPath=@Icon}"></Image>
</StackPanel>
и то же самое со свойством Text TextBlocks
.
Я получаю следующее исключение в окне вывода, когда нажимаю на элемент в виде дерева ...
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'