2011-04-16 85 views
1

我有一個TreeView,其數據上下文設置使用結合WPF樹視圖模板項目

 LayoutRoot.DataContext = value 

從代碼隱藏。

CommandTreeViewModelIEnumerable(Of CommandViewModel)

CommandViewModel又一個Commands屬性具有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> 

我已經在其他地方獲得了圖像和兩個文本塊,我想將它們綁定到所選樹視圖項的原始數據源上的元素 - s特別是,Icon,Description,Name。我試圖將它們綁定,如下圖所示:

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

,並與TextBlocks的Text屬性是相同的。

我得到在輸出窗口下面的異常,當我點擊一個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' 

回答

1

你的XAML使用在綁定的XPath其綁定到XML文檔時才使用:

{Binding [email protected]} 

試試這個:

{Binding Path=Icon} 

有可能會是Ë我們也繼續。

+0

謝謝,解決了它 – Basic 2011-04-17 02:30:24

1

您是否基於某些XML構建了CommandViewModel層次結構?因爲如果是這樣的話你的CommandViewModel也應該有像XmlNode SourceNode屬性鏈接到相應的XmlNode,然後在XAML你應該做的:

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

那麼它應該工作。

或者,如果你不使用XML的所有的一切,因爲StellarEleven建議 - 從{Binding [email protected]}刪除「X」和「@」。

+0

很好的建議,不幸的是,恆星是正確的。感謝您的幫助! – Basic 2011-04-17 02:30:49