我想要可编辑的根 TreeViewItem,为此我创建了事件
<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>
在 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() };
}
但是我有两个问题:
1)此代码不会更改 TextBlock 上的 Header
2)这个XAML在所有项目上设置事件,我只想编辑根项目......
感谢帮助!