Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess XAML controls from C# code-behind
    primarykey
    data
    text
    <p>I have two files in my VS project: <code>Custom.xaml</code> and <code>Custom.cs</code></p> <p>In my <em>XAML</em> file, I have the following text boxes:</p> <pre><code>&lt;TextBox x:Name="TextBox1" Text="{Binding Value, Mode=TwoWay}" Foreground="Black" Background="Green" SelectionChanged="TextBox1_SelectionChanged" /&gt; &lt;TextBox x:Name="TextBox2" Text="{Binding Value, Mode=TwoWay}" Foreground="Black" Background="Green" SelectionChanged="TextBox2_SelectionChanged" /&gt; </code></pre> <p>In my <em>.cs</em>, I have the following method:</p> <pre><code> void TextBox1_SelectionChanged(object sender, RoutedEventArgs e) { TextBox t = e.Source as TextBox } </code></pre> <p>I can successfully hit the event handler above. Then, I can grab <code>TextBox1</code> and it's properties by using <code>e.Source</code>, but I would like to access TextBox2 and it's properties.</p> <p>As a sidenote, the <em>.cs</em> file is just a <code>C#</code> class that I am referencing, not a <em>xaml.cs</em>. Additionally, I understand that I could implement this via a <em>UserControl</em>, but cannot do that in this scenario for reasons that are outside the scope of this post.</p> <p>Please advise on how I can get/set properties of <code>TextBox2</code>.</p> <p>Thanks.</p> <p>EDIT: Any other input on this? As a workaround, I've added an event handler called TextBox2_Loaded, and then set e.Source to an instance variable. Then, in TextBox1_SelectionChanged, I can access the instance variable. Would really like to just target the control directly (ex. TextBox2.IsEnabled). I must be missing a declaration or inheritance somewhere. Can't even find the control using FindName.</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.
 

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