在XAML中访问codebehind变量 [英] Access codebehind variable in XAML
本文介绍了在XAML中访问codebehind变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何才能获得这Sample.xaml.cs文件中像asp.net&LT公共变量;?%= VARIABLENAME%>
How can i access public variable which in Sample.xaml.cs file like asp.net <%=VariableName%> ?
推荐答案
有几个方法可以做到这一点。
There are a few ways to do this.
-
添加您的变量从codebehind资源:
Add your variable as a resource from codebehind:
myWindow.Resources.Add("myResourceKey", myVariable);
然后就可以从XAML访问:
Then you can access it from XAML:
<TextBlock Text="{StaticResource myResourceKey}"/>
如果您要添加的XAML被解析后,您可以使用 DynamicResource
上面,而不是静态资源
If you have to add it after the XAML gets parsed, you can use a DynamicResource
above instead of StaticResource
.
请变量在你的XAML东西的属性。通常这是通过的DataContext
:
Make the variable a property of something in your XAML. Usually this works through the DataContext
:
myWindow.DataContext = myVariable;
或
myWindow.MyProperty = myVariable;
在此之后,在你的XAML什么都可以通过访问一个绑定
:
<TextBlock Text="{Binding Path=PropertyOfMyVariable}"/>
或
<TextBlock Text="{Binding ElementName=myWindow, Path=MyProperty}"/>
这篇关于在XAML中访问codebehind变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文