0
votes

I want editable root TreeViewItem, for that I create Event

      <TreeView Height="182" Name="infoBox_treeview" Margin="0,2,0,182" BorderBrush="#FFA5A5A5" BorderThickness="2">
            <TreeViewItem Header="t1"></TreeViewItem>
            <TreeViewItem Header="t2">
                <TreeViewItem Header="t21"></TreeViewItem>
                <TreeViewItem Header="t22"></TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="t3"></TreeViewItem>
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <EventSetter Event="MouseDoubleClick" Handler="infobox_item_edit"></EventSetter>
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.ContextMenu>
                <ContextMenu Name="infoBox_treeview_menu">
                    <MenuItem Header="Добавить ссылку" Name="infobox_menu_addlink" Click="infobox_menu_addlink_Click"></MenuItem>
                    <MenuItem Header="Удалить ссылку" Name="infobox_menu_dellink"></MenuItem>
                </ContextMenu>
            </TreeView.ContextMenu>
        </TreeView>

In C#:

private void infobox_item_edit(object sender, RoutedEventArgs e)
{
    var selItem=(TreeViewItem)this.infoBox_treeview.SelectedItem;
    selItem.Header = new TextBlock() { Text = selItem.Header.ToString() };
}

But I have two problems:

1) This code doesn't change Header on TextBlock

2) This XAML set Event on all Items, I want edit only root items...

Thanks for help!

2

2 Answers

0
votes

1) You require 2 events to handle the edit and save part .... you have written the code for saving in the edit part . use OnTreeViewSelectedItemChanged to capture the part when the item is changed.
Please check this link Create Event Handler for TreeViewItem in WPF
2) What do you mean by root , only the topmost node or node which has children?

0
votes

You can style parent nodes in a TreeView differently than child nodes using a HierarchicalDataTemplate. Using this method, you can add your event handlers to each parent node and not the child only nodes. You can find out more from this page on MSDN.