2011-04-16 6 views
1

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' 

Répondre

1

Votre XAML utilise un XPath dans la liaison qui est utilisée uniquement lors de la liaison à un document XML :

{Binding [email protected]} 

Essayez ceci:

{Binding Path=Icon} 

Il pourrait y avoir quelque chose e Je continue aussi.

+0

Merci, cela a résolu – Basic

1

Construisez-vous votre hiérarchie CommandViewModel basée sur du code XML? Car si tel est le cas de votre CommandViewModel devrait également avoir une propriété comme XmlNode SourceNode qui relie la XmlNode correspondante puis dans votre XAML vous devez faire:

 <StackPanel Orientation="Vertical" 
      DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem.SourceNode}"> 
      <Image x:Name="CommandIcon" 
        Width="64" 
        Height="64" 
        Source="{Binding [email protected]}"></Image> 
     </StackPanel> 

alors il devrait fonctionner.

Ou si vous n'utilisez pas du tout le format XML, faites comme StellarEleven suggéré - supprimer les "X" et "@" de {Binding [email protected]}.

+0

Bonne suggestion, malheureusement, Stellar avait raison. Merci pour l'aide en tout cas! – Basic