开发者

WPF reference object from XAML

开发者 https://www.devze.com 2023-02-15 03:53 出处:网络
Here is one that bothers me! I found on stackoverflow this Accessing XAML object in codebehind(WPF) about accessing a resource.

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" .../>
0

精彩评论

暂无评论...
验证码 换一张
取 消