Reputation: 38094
I would like to enable editing an item in TreeView
just in two cases:
Edit
button in ContextMenu
of TreeView
F2
at the selected item of TreeView.My xaml of TreeView
:
<TreeView ItemsSource="{Binding FooColl}" >
<TreeView.Resources>
<DiscreteObjectKeyFrame x:Key="proxy" Value="{Binding}"/>
<HierarchicalDataTemplate DataType="{x:Type treeViewModel:NodeViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Source="treeNode.png" />
<TextBlock Text="{Binding FooValue}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
My first thought was to use TextBox
instead of TextBlock
in HierarchicalDataTemplate
. However, the edit mode of TextBox
is enabled by MouseClick
. Consequently, it is not what I want.
Any thoughts about how can I do that?
Upvotes: 0
Views: 168
Reputation: 1336
You can use IsReadOnly property:
<TextBlock Text="{Binding FooValue}" IsReadOnly="{Binding ImReadOnly}">
And to handle F2 key-press, you can try this :
public partial class MyView : UserControl
{
public MyView()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(KeyDownEvent);
}
private void KeyDownEvent(object sender, KeyEventArgs e)
{
try
{
switch (e.Key)
{
case Key.F2:
var vm = this.DataContext as YourViewModel;
vm.YourCommand.Execute(null);
break;
}
}
catch (Exception ex)
{
//...
}
}
}
Upvotes: 1