Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What I've done in these situations is used the XAML hierarchy to determine which element to show/hide. Something along the lines of: </p> <pre><code>&lt;Grid&gt; &lt;TextBlock MouseDown="txtblk_MouseDown" /&gt; &lt;TextBox LostFocus="txtbox_LostFocus" Visibility="Collapsed" /&gt; &lt;/Grid&gt; </code></pre> <p>with the code:</p> <pre><code>protected void txtblk_MouseDown(object sender, MouseButtonEventArgs e) { TextBox txt = (TextBox)((Grid)((TextBlock)sender).Parent).Children[1]; txt.Visibility = Visibility.Visible; ((TextBlock)sender).Visibility = Visibility.Collapsed; } protected void txtbox_LostFocus(object sender, RoutedEventArgs e) { TextBlock tb = (TextBlock)((Grid)((TextBox)sender).Parent).Children[0]; tb.Text = ((TextBox)sender).Text; tb.Visibility = Visibility.Visible; ((TextBox)sender).Visibility = Visibility.Collapsed; } </code></pre> <p>I always turn stuff like this that I'm going to reuse into a <code>UserControl</code>, which I can add additional error handling to, and guarantee that the <code>Grid</code> will only contain two items, and the order of them will never change.</p> <p>EDIT: Additionally, turning this into a UserControl allows you to create a <code>Text</code> property for each instantiation, so you can name each one and reference the text directly without fishing for the current value through the <code>((TextBox)myGrid.Children[1]).Text</code> casting. This will make your code much more efficient and clean. If you make it into a UserControl, you can also name the <code>TextBlock</code> and <code>TextBox</code> elements, so no casting is needed at all.</p>
 

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