In my WPF C# project, I've created a TreeView. Each TreeViewItem has a LostFocus event that must be raised when item lost its focus.
I've also create a button that is needed to be clicked when user wants to change header of a certain TreeViewItem.
User, after the selection in TreeView, can click on button and a TextBox appear replacing the TreeViewItem header.
If user does not click on TextBox but click on another TreeViewItem, the LostFocus event is never raised. Otherwise, if user click on TextBox and then change focus, it is raised.
I've also used textBox.Focus()
and Keyboard.Focus(textBox)
but the do not work.
How can I fix this?
Just to be clear, before creating a post I've read another SO answer here
Here is the snippet code
private void RenameButton_Click(object sender, RoutedEventArgs e)
{
TreeViewItem twItemSelected = (TreeViewItem)this.Treeview_PropertyDefinition.SelectedItem;
var textBox = new TextBox()
{
Text = (String)twItemSelected.Header,
};
textBox.Focus();
Keyboard.Focus(textBox);
if (textBox.IsFocused)
MessageBox.Show("focused");
twItemSelected.Header = textBox;
//check which property is currently selected
String parentName = ((TreeViewItem)twItemSelected.Parent).Name;
((TreeViewItem)twItemSelected.Parent).Parent).Name;
//get values from file
//show page based on parent value
switch (parentName)
{
case "RectangleBar_TreeviewItem":
textBox.LostFocus += (o, ev) =>
{...}
}