J'ai un TreeView dont le contexte des données est définie à l'aideReliure à WPF TreeView article basé sur un modèle
LayoutRoot.DataContext = value
du code-behind.
CommandTreeViewModel
a une propriété Commands
de IEnumerable(Of CommandViewModel)
CommandViewModel
à son tour a plusieurs enfants de CommandViewModel
Mon XAML, je convertir en objets d'arbre en utilisant le code XAML suivant
<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>
Maintenant, je 'ai une image et deux blocs de texte ailleurs que je veux lier aux éléments sur la source de données originale pour l'élément de treeview sélectionné - s spécifiquement, Icon
, Description
, Name
. Je tente de les lier comme indiqué ci-dessous:
<StackPanel Orientation="Vertical"
DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem}">
<Image x:Name="CommandIcon"
Width="64"
Height="64"
Source="{Binding [email protected]}"></Image>
</StackPanel>
et la même chose avec la propriété Text de TextBlocks
.
Je reçois l'exception suivante dans la fenêtre de sortie lorsque je clique sur un élément TreeView ...
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'
Merci, cela a résolu – Basic