如何将xaml属性绑定到另一个类中的静态变量? [英] How can I bind a xaml property to a static variable in another class?
问题描述
我有这个xaml文件,我试图在其中将文本块背景绑定到另一个类中的静态变量,该如何实现呢?
I have this xaml file in which I try to bind a Text-block Background to a static variable in another class, how can I achieve this ?
我知道这可能很愚蠢,但是我刚从Win-forms搬走了,感到有点失落.
I know this might be silly but I just moved from Win-forms and feeling a little bit lost.
这是我的意思:
<TextBlock Text="some text"
TextWrapping="WrapWithOverflow"
Background="{Binding Path=SomeVariable}" />
推荐答案
首先,您无法绑定到变量
.您只能从XAML绑定到 properties
.对于绑定到静态属性,您可以采用这种方式(例如,您要绑定 TextBlock
的 Text
属性)-
First of all you can't bind to variable
. You can bind only to properties
from XAML.
For binding to static property you can do in this way (say you want to bind Text
property of TextBlock
) -
<TextBlock Text="{Binding Source={x:Static local:YourClassName.PropertyName}}"/>
其中 local
是您的类所在的名称空间,您需要在上面的xaml文件中像下面这样声明-
where local
is namespace where your class resides which you need to declare above in xaml file like this -
xmlns:local="clr-namespace:YourNameSpace"
这篇关于如何将xaml属性绑定到另一个类中的静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!