[英]Access XAML controls from C# code-behind
I have two files in my VS project: Custom.xaml
and Custom.cs
我的VS项目中有两个文件:
Custom.xaml
和Custom.cs
In my XAML file, I have the following text boxes: 在我的XAML文件中,我有以下文本框:
<TextBox x:Name="TextBox1" Text="{Binding Value, Mode=TwoWay}" Foreground="Black" Background="Green" SelectionChanged="TextBox1_SelectionChanged" />
<TextBox x:Name="TextBox2" Text="{Binding Value, Mode=TwoWay}" Foreground="Black" Background="Green" SelectionChanged="TextBox2_SelectionChanged" />
In my .cs , I have the following method: 在我的.cs中 ,我有以下方法:
void TextBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
TextBox t = e.Source as TextBox
}
I can successfully hit the event handler above. 我可以成功点击上面的事件处理程序。 Then, I can grab
TextBox1
and it's properties by using e.Source
, but I would like to access TextBox2 and it's properties. 然后,我可以通过使用
e.Source
获取TextBox1
及其属性,但我想访问TextBox2及其属性。
As a sidenote, the .cs file is just a C#
class that I am referencing, not a xaml.cs . 作为旁注, .cs文件只是我引用的
C#
类,而不是xaml.cs。 Additionally, I understand that I could implement this via a UserControl , but cannot do that in this scenario for reasons that are outside the scope of this post. 另外,我知道我可以通过UserControl实现这个,但是由于这篇文章范围之外的原因,在这种情况下不能这样做。
Please advise on how I can get/set properties of TextBox2
. 请告诉我如何获取/设置
TextBox2
属性。
Thanks. 谢谢。
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.
作为一种解决方法,我添加了一个名为TextBox2_Loaded的事件处理程序,然后将e.Source设置为实例变量。 Then, in TextBox1_SelectionChanged, I can access the instance variable.
然后,在TextBox1_SelectionChanged中,我可以访问实例变量。 Would really like to just target the control directly (ex. TextBox2.IsEnabled).
真的想直接定位控件(例如TextBox2.IsEnabled)。 I must be missing a declaration or inheritance somewhere.
我必须在某个地方错过声明或继承。 Can't even find the control using FindName.
甚至无法使用FindName找到控件。
As long as you have set a namein the XAML, you can access it directly by name (The XAML compiler will create an instance variable for you.) 只要您在XAML中设置了名称,就可以直接通过名称访问它(XAML编译器将为您创建一个实例变量。)
void TextBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
TextBox t = e.Source as TextBox
TextBox2.Text = "Whatever";
}
Alright, so I apparently had left out a critical component in this post... My TextBox controls are inside of DataTemplate controls. 好吧,所以我显然在这篇文章中遗漏了一个关键组件......我的TextBox控件位于DataTemplate控件中。 From my research, the TextBox controls cannot be accessed when inside of DataTemplate controls.
根据我的研究,在DataTemplate控件内部时无法访问TextBox控件。 I really didn't think that would matter, but I guess the instance variables are not created when this scenario exists.
我真的不认为这很重要,但我想当这种情况存在时,不会创建实例变量。
If I've interpreted this incorrectly, please provide input. 如果我错误地解释了这一点,请提供输入。 For now, I've gone ahead and added a Loaded event and defined my TextBox controls as instance variables so that I can access them and change properties when other activities occur.
现在,我已经继续添加了一个Loaded事件并将我的TextBox控件定义为实例变量,以便我可以访问它们并在发生其他活动时更改属性。
Thanks for everyone's input. 感谢大家的投入。
This just happened to me. 这恰好发生在我身上。 I had to close my solution and reopen it.
我不得不关闭我的解决方案并重新打开它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.