WPF reference object from XAML
Here is one that bothers me!
I found on stackoverflow this Accessing XAML object in codebehind(WPF) about accessing a resource.
But what if i want re开发者_运维技巧ference to a textbox, for example, in my code-behind file? I didn't find that around.
Thanks!
Give it an x:Name
and then in your code-behind just reference it as this.TheName
.
See XAML Named Elements on MSDN.
XAML:
<TextBox Name="txtBox" Text="Example Text"/>
Code:
txtBox.Foreground = Brushes.Blue;
But not all objects in xaml has property called Name
, in that case you can use this:
<SomeObject x:Name="namedObject" .../>
精彩评论