开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜