Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This article should get you started : <a href="http://weblogs.asp.net/thomaslebrun/archive/2009/03/24/wpf-how-to-develop-and-editable-textblock.aspx" rel="nofollow">http://weblogs.asp.net/thomaslebrun/archive/2009/03/24/wpf-how-to-develop-and-editable-textblock.aspx</a></p> <p>Here is an adaptation, you would need to add the value binding part of the TextBox and RichTextBox :</p> <p>XAML</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;Style TargetType="{x:Type local:EditableTextBlock}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:EditableTextBlock}"&gt; &lt;Grid x:Name="PART_GridContainer"&gt; &lt;RichTextBox x:Name="PART_TbDisplayText" Visibility="Visible" /&gt; &lt;TextBox x:Name="PART_TbEditText" Visibility="Hidden" /&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style TargetType="TextBox"&gt; &lt;Setter Property="BorderBrush" Value="Red" /&gt; &lt;/Style&gt; &lt;Style TargetType="RichTextBox"&gt; &lt;Setter Property="BorderBrush" Value="Blue" /&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;StackPanel&gt; &lt;local:EditableTextBlock /&gt; &lt;Button&gt;Lose focus&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p>Code:</p> <pre><code>[TemplatePart(Type = typeof(Grid), Name = EditableTextBlock.GRID_NAME)] [TemplatePart(Type = typeof(RichTextBox), Name = EditableTextBlock.RICHTEXTBOX_DISPLAYTEXT_NAME)] [TemplatePart(Type = typeof(TextBox), Name = EditableTextBlock.TEXTBOX_EDITTEXT_NAME)] public class EditableTextBlock : Control { private const string GRID_NAME = "PART_GridContainer"; private const string RICHTEXTBOX_DISPLAYTEXT_NAME = "PART_TbDisplayText"; private const string TEXTBOX_EDITTEXT_NAME = "PART_TbEditText"; private Grid gridContainer; private RichTextBox richTextBox; private TextBox textBox; static EditableTextBlock() { DefaultStyleKeyProperty.OverrideMetadata(typeof(EditableTextBlock), new FrameworkPropertyMetadata(typeof(EditableTextBlock))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); this.gridContainer = this.Template.FindName(GRID_NAME, this) as Grid; if (this.gridContainer != null) { this.richTextBox = this.gridContainer.Children[0] as RichTextBox; this.richTextBox.GotFocus += new RoutedEventHandler(richTextBoxGotFocus); this.textBox = this.gridContainer.Children[1] as TextBox; this.textBox.LostFocus += this.textBoxLostFocus; } } private void richTextBoxGotFocus(object sender, RoutedEventArgs e) { this.richTextBox.Visibility = Visibility.Hidden; this.textBox.Visibility = Visibility.Visible; this.textBox.Focus(); } private void textBoxLostFocus(object sender, RoutedEventArgs e) { this.richTextBox.Visibility = Visibility.Visible; this.textBox.Visibility = Visibility.Hidden; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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