2015-09-09 37 views
0

現在我有樹視圖是呈現通用的XML:如何讓TreeView呈現通用xml可編輯?

<TreeView Name="myTreeView" 
        ItemTemplate="{StaticResource treeViewTemplate}"/> 


<MyApp.Resources> 
     <SolidColorBrush Color="Blue" x:Key="xmlValueBrush"/> 
     <SolidColorBrush Color="Red" x:Key="xmAttributeBrush"/> 
     <SolidColorBrush Color="DarkMagenta" x:Key="xmlTagBrush"/> 
     <SolidColorBrush Color="Blue" x:Key="xmlMarkBrush"/> 

     <DataTemplate x:Key="attributeTemplate"> 
      <StackPanel Orientation="Horizontal" 
         Margin="3,0,0,0" HorizontalAlignment="Center"> 
       <TextBlock Text="{Binding Path=Name}" 
          Foreground="{StaticResource xmAttributeBrush}"/> 
       <TextBlock Text="=&quot;" 
          Foreground="{StaticResource xmlMarkBrush}"/> 
       <TextBlock Text="{Binding Path=Value}" 
          Foreground="{StaticResource xmlValueBrush}"/> 
       <TextBlock Text="&quot;" 
          Foreground="{StaticResource xmlMarkBrush}"/> 
      </StackPanel> 
     </DataTemplate> 

     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="IsExpanded" Value="True"/> 
      <Setter Property="IsManipulationEnabled" Value="True"/> 
     </Style> 

     <HierarchicalDataTemplate x:Key="treeViewTemplate" 
            ItemsSource="{Binding XPath=child::node()}"> 

      <StackPanel Orientation="Horizontal" Margin="3,0,0,0" 
         HorizontalAlignment="Center"> 
       <TextBlock Text="&lt;" HorizontalAlignment="Center" 
          Foreground="{StaticResource xmlMarkBrush}" 
          x:Name="startTag"/> 

       <TextBlock Text="{Binding Path=Name}" 
        Margin="0" 
        HorizontalAlignment="Center" 
        x:Name="xmlTag" 
        Foreground="{StaticResource xmlTagBrush}"/> 

       <ItemsControl 
        ItemTemplate="{StaticResource attributeTemplate}" 
        ItemsSource="{Binding Path=Attributes}" 
        HorizontalAlignment="Center"> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <StackPanel Orientation="Horizontal"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
       </ItemsControl> 

       <TextBlock Text="&gt;" HorizontalAlignment="Center" 
          Foreground="{StaticResource xmlMarkBrush}" 
          x:Name="endTag"/> 
      </StackPanel> 

      <HierarchicalDataTemplate.Triggers> 
       <DataTrigger Binding="{Binding NodeType}"> 
        <DataTrigger.Value> 
         <xmlNodes:XmlNodeType>Text</xmlNodes:XmlNodeType> 
        </DataTrigger.Value> 
        <Setter Property="Text" Value="{Binding InnerText}" 
          TargetName="xmlTag"/> 
        <Setter Property="Foreground" Value="Blue" 
          TargetName="xmlTag"/> 
        <Setter Property="Visibility" Value="Collapsed" 
          TargetName="startTag"/> 
        <Setter Property="Visibility" Value="Collapsed" 
          TargetName="endTag"/> 

       </DataTrigger> 

       <DataTrigger Binding="{Binding HasChildNodes}" Value="False"> 
        <Setter Property="Text" Value="/&gt;" TargetName="endTag"/> 
       </DataTrigger> 
      </HierarchicalDataTemplate.Triggers> 
     </HierarchicalDataTemplate> 

    </MyApp.Resources> 

在代碼隱藏XML被綁定到TreeView的是這樣的:

myTreeView.SetBinding(TreeView.ItemsSourceProperty, binding); //binding is binded XMLDataProvider 

我怎樣才能使這個樹視圖編輯。例如,如果我有這個xml:

<note> 
    <to>Tove</to> 
    <from>Jani</from> 
    <heading>Reminder</heading> 
    <body>Don't forget me this weekend!</body> 
</note> 

我想使標籤內的值如:Tove,Jani,提醒可編輯。

有人可以幫我嗎?

回答

0

你可以嘗試設置treeView.LabelEdit爲true:

treeView.LabelEdit = true; 

當此設置,你應該能夠通過單擊編輯節點的文本。您還可以使用

treeView.Nodes[0].BeginEdit(); 

編輯特定節點。上面的代碼將開始編輯根節點。

+0

我試過,但我很奇怪,我沒有在我的樹形視圖中的那些屬性,3最上面的是SelectedItem,SelectedValue和SelectedValuePath。 – UnderNotic

+0

對不起,我假設你使用的是WinForms。不知道如何在wpf中做到這一點,不幸的是... – Tobbe