I’m trying to create a TreeView which allows the user to rename the nodes in the TreeView. The tree represents an HL7 message and is structured from segment to subcomponent hierarchically.
For example:
PID
PID.1
PID.2
etc...
I need to allow the user to select a node, press F2 to put the node into edit mode. Because HL7 allows repeating message structures, I also need the SelectedItem so I can know which node was changed in case duplicate names exist.
Currently, each node is a TextBox with IsReadOnly set to true and is stylized to look like a TextBlock. When the user presses F2, I stylize the TextBox to look like it normally does for input. The problem is, the TextBox is eating all the mouse events preventing the TreeView from setting SelectedItem or raising SelectedItemChanged.
I found some discussion on MSDN where one person says use the PreviewMouseLeftButtonDown event on the TextBox. I’m using that and the TextBox is still consuming the event.
Has anyone run into this before or have any suggestions?
Another way is to have a TextBlock for display and a hidden TextBox for editing. Listen for F2 on the TreeView which will receive the keyboard events since the TextBox won’t be getting any input focus while it is hidden. When F2 is pressed, hide the TextBlock and show the TextBox for editing. Handle the LostFocus event on the TextBox to hide the TextBox and show the TextBlock again.
One advantage of doing it this way is you don’t have to fake a TextBox into looking and behaving like a TextBlock. The TextBlock will always look and behave like a TextBlock and the TextBox will always look and behave like a TextBox, and they will each be able to inherit any styling applied at a higher resource level.
Edit: Adding some sample code.
Here is the XAML:
Here is the code behind:
This code assumes your HL7Object is the base class for your data objects, such as the following:
And also that you have implemented a DataTemplateSelector, which I assume you have because of your complex requirements. If not, here is an example: