Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>One advantage of doing it this way is you don't have to <em>fake</em> 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.</p> <p>Edit: Adding some sample code.</p> <p>Here is the XAML:</p> <pre><code>&lt;Window.Resources&gt; &lt;Style x:Key="TreeViewTextBlockStyle" TargetType="TextBlock"&gt; &lt;Setter Property="Text" Value="{Binding DisplayText}"/&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding InEditMode}" Value="true"&gt; &lt;Setter Property="Visibility" Value="Collapsed"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;Style x:Key="TreeViewTextBoxStyle" TargetType="TextBox"&gt; &lt;Setter Property="Text" Value="{Binding DisplayText, Mode=TwoWay}"/&gt; &lt;Setter Property="MinWidth" Value="50"/&gt; &lt;EventSetter Event="LostFocus" Handler="TreeViewTextBox_LostFocus" /&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding InEditMode}" Value="false"&gt; &lt;Setter Property="Visibility" Value="Collapsed"/&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding InEditMode}" Value="true"&gt; &lt;Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Mode=Self}}"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;HierarchicalDataTemplate x:Key="HL7MessageTemplate" ItemsSource="{Binding Segments}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="[msg]--" FontWeight="Bold"/&gt; &lt;TextBlock Style="{StaticResource TreeViewTextBlockStyle}"/&gt; &lt;TextBox Style="{StaticResource TreeViewTextBoxStyle}" /&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;DataTemplate x:Key="HL7SegmentTemplate"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="[seg]--" FontWeight="Bold"/&gt; &lt;TextBlock Style="{StaticResource TreeViewTextBlockStyle}"/&gt; &lt;TextBox Style="{StaticResource TreeViewTextBoxStyle}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;HierarchicalDataTemplate x:Key="HL7SegmentWithSubcomponentsTemplate" ItemsSource="{Binding Subcomponents}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="[seg+sub]--" FontWeight="Bold"/&gt; &lt;TextBlock Style="{StaticResource TreeViewTextBlockStyle}"/&gt; &lt;TextBox Style="{StaticResource TreeViewTextBoxStyle}" /&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;DataTemplate x:Key="HL7SubcomponentTemplate"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="[sub]--" FontWeight="Bold"/&gt; &lt;TextBlock Style="{StaticResource TreeViewTextBlockStyle}"/&gt; &lt;TextBox Style="{StaticResource TreeViewTextBoxStyle}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;local:HL7DataTemplateSelector x:Key="HL7DataTemplateSelector"/&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;TreeView Name="treeView1" ItemsSource="{Binding}" ItemTemplateSelector="{StaticResource HL7DataTemplateSelector}" KeyUp="treeView1_KeyUp"/&gt; &lt;/Grid&gt; </code></pre> <p>Here is the code behind:</p> <pre><code>private void treeView1_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.F2) { HL7Object selectedHL7Object = treeView1.SelectedItem as HL7Object; if (selectedHL7Object != null) { selectedHL7Object.InEditMode = true; } } } private void TreeViewTextBox_LostFocus(object sender, RoutedEventArgs e) { HL7Object selectedHL7Object = treeView1.SelectedItem as HL7Object; if (selectedHL7Object != null) { selectedHL7Object.InEditMode = false; } } </code></pre> <p>This code assumes your HL7Object is the base class for your data objects, such as the following:</p> <pre><code>public class HL7Object : INotifyPropertyChanged { private string DisplayTextField; public string DisplayText { get { return this.DisplayTextField; } set { if (this.DisplayTextField != value) { this.DisplayTextField = value; this.OnPropertyChanged("DisplayText"); } } } private bool InEditModeField = false; public bool InEditMode { get { return this.InEditModeField; } set { if (this.InEditModeField != value) { this.InEditModeField = value; this.OnPropertyChanged("InEditMode"); } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } </code></pre> <p>And also that you have implemented a DataTemplateSelector, which I assume you have because of your complex requirements. If not, here is an example:</p> <pre><code>public class HL7DataTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { FrameworkElement element = container as FrameworkElement; if (element != null &amp;&amp; item != null &amp;&amp; (item is HL7Message || item is HL7Segment || item is HL7Subcomponent) ) { HL7Message message = item as HL7Message; if (message != null) { return element.FindResource("HL7MessageTemplate") as DataTemplate; } HL7Segment segment = item as HL7Segment; if (segment != null) { if (segment.Subcomponents != null &amp;&amp; segment.Subcomponents.Count &gt; 0) { return element.FindResource("HL7SegmentWithSubcomponentsTemplate") as DataTemplate; } else { return element.FindResource("HL7SegmentTemplate") as DataTemplate; } } HL7Subcomponent subcomponent = item as HL7Subcomponent; if (subcomponent != null) { return element.FindResource("HL7SubcomponentTemplate") as DataTemplate; } } return null; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload