Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The ideal way to do this would be to create a <code>ClickEditableTextBlock</code> control, which by default renders as a TextBlock but shows a TextBox when the user clicks it. Because any given ClickEditableTextBlock has only one TextBlock and one TextBox, you don't have the matching issue. Then you use a ClickEditableTextBlock instead of separate TextBlocks and TextBoxes in your DataTemplate.</p> <p>This has the side benefit of encapsulating the functionality in a control so you don't pollute your main window code-behind with the edit behaviour, plus you can easily reuse it in other templates.</p> <hr> <p>If this sounds like too much effort, you can use Tag or an attached property to associate each TextBlock with a TextBox:</p> <pre><code>&lt;DataTemplate&gt; &lt;StackPanel&gt; &lt;TextBlock Text="whatever" MouseDown="TextBlock_MouseDown" Tag="{Binding ElementName=tb}" /&gt; &lt;TextBox Name="tb" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>Note the use of <code>{Binding ElementName=tb}</code> on the Tag to refer to the TextBox named <code>tb</code>.</p> <p>And in your code-behind:</p> <pre><code>private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e) { FrameworkElement textBlock = (FrameworkElement)sender; TextBox editBox = (TextBox)(textBlock.Tag); editBox.Text = "Wow!"; // or set visible or whatever } </code></pre> <p>(To avoid the use of the nasty Tag property, you could define a custom attached property to carry the TextBox binding, but for brevity I'm not showing that.)</p>
    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. 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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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