如何将xaml属性绑定到另一个类中的静态变量?

时间:2021-01-18 21:32:01

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 ?

在xaml文件中,我尝试将文本块背景绑定到另一个类中的静态变量,我如何实现这一点?

I know this might be silly but i just moved from Win-forms and feeling a little bit lost.

我知道这可能很愚蠢,但我刚从赢球模式中走出来,感觉有点失落。

here is what i mean :

我的意思是:

   <TextBlock Text="some text"
                             TextWrapping="WrapWithOverflow"
                             Foreground="{Binding Path=SomeVariable}" />

3 个解决方案

#1


40  

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) -

首先你不能绑定到变量。只能绑定XAML中的属性。要绑定到静态属性,您可以这样做(假设您想绑定TextBlock的文本属性)-

<TextBlock Text="{Binding Source={x:Static local:YourClassName.PropertyName}}"/>

where local is namespace where your class resides which you need to declare above in xaml file like this -

在xaml文件中需要声明的类所在的本地命名空间是什么

xmlns:local="clr-namespace:YourNameSpace"

#2


7  

You can't actually bind to a static property (INotifyPropertyChanged makes sense on instances only), so this should be enough...

实际上,您不能绑定到静态属性(仅在实例中修改INotifyPropertyChanged才有意义),因此这应该足够……

{x:Static my:MyTestStaticClass.MyProperty}  

or e.g.

或如。

<TextBox Text="{x:Static my:MyTestStaticClass.MyProperty}" Width="500" Height="100" />  

make sure you include the namespace - i.e. define the my in the XAML like xmlns:my="clr-namespace:MyNamespace"

确保包含名称空间——例如在XAML中定义my,如xmlns:my="clr-namespace:MyNamespace"


EDIT: binding from code
(There're some mixed answers on this part so I thought it made sense to expand, have it in one place)

编辑:从代码绑定(这部分有一些复杂的答案,所以我认为扩展它是有意义的,把它放在一个地方)


OneTime binding:

You could just use textBlock.Text = MyStaticClass.Left (just careful where you place that, post-init)

你可以用textBlock。= MyStaticClass文本。左(注意你的位置,post-init)

TwoWay (or OneWayToSource) binding:

Binding binding = new Binding();
//binding.Source = typeof(MyStaticClass);
// System.InvalidOperationException: 'Binding.StaticSource cannot be set while using Binding.Source.'
binding.Path = new PropertyPath(typeof(MyStaticClass).GetProperty(nameof(MyStaticClass.Left)));
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
this.SetBinding(Window.LeftProperty, binding);

...of course if you're setting Binding from the code remove any bindings in XAML.

…当然,如果要从代码中设置绑定,请删除XAML中的任何绑定。

OneWay (property changes from the source):

And if you'd need to update the target (i.e. the control's property, Window.Left in this case) on the source property changes, that can't be achieved with the static class (as per my comment above, you'd need the INotifyPropertyChanged implemented, so you could just use a wrapper class, implement INotifyPropertyChanged and wire that to a static property of your interest (providing you know how to track you static property's changes, i.e. this is more of a 'design' issue from this point on, I'd suggest redesigning and putting it all within one 'non-static' class).

如果您需要更新目标(即控件的属性,窗口)。左在这种情况下)的源属性更改,与静态类不能实现(按我上面的评论,你需要实现INotifyPropertyChanged,所以你可以使用一个包装器类,实现INotifyPropertyChanged线,你感兴趣的静态属性(提供你知道如何追踪你静态属性的变化,即这更多的是一种“设计”问题从这一点,我建议重新设计,并把这些都在一个非静态的类)。

#3


0  

You can use the newer x:Bind to do this simply using:

您可以使用较新的x:Bind吗?

<TextBlock Text="{x:Bind YourClassName.PropertyName}"/>

#1


40  

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) -

首先你不能绑定到变量。只能绑定XAML中的属性。要绑定到静态属性,您可以这样做(假设您想绑定TextBlock的文本属性)-

<TextBlock Text="{Binding Source={x:Static local:YourClassName.PropertyName}}"/>

where local is namespace where your class resides which you need to declare above in xaml file like this -

在xaml文件中需要声明的类所在的本地命名空间是什么

xmlns:local="clr-namespace:YourNameSpace"

#2


7  

You can't actually bind to a static property (INotifyPropertyChanged makes sense on instances only), so this should be enough...

实际上,您不能绑定到静态属性(仅在实例中修改INotifyPropertyChanged才有意义),因此这应该足够……

{x:Static my:MyTestStaticClass.MyProperty}  

or e.g.

或如。

<TextBox Text="{x:Static my:MyTestStaticClass.MyProperty}" Width="500" Height="100" />  

make sure you include the namespace - i.e. define the my in the XAML like xmlns:my="clr-namespace:MyNamespace"

确保包含名称空间——例如在XAML中定义my,如xmlns:my="clr-namespace:MyNamespace"


EDIT: binding from code
(There're some mixed answers on this part so I thought it made sense to expand, have it in one place)

编辑:从代码绑定(这部分有一些复杂的答案,所以我认为扩展它是有意义的,把它放在一个地方)


OneTime binding:

You could just use textBlock.Text = MyStaticClass.Left (just careful where you place that, post-init)

你可以用textBlock。= MyStaticClass文本。左(注意你的位置,post-init)

TwoWay (or OneWayToSource) binding:

Binding binding = new Binding();
//binding.Source = typeof(MyStaticClass);
// System.InvalidOperationException: 'Binding.StaticSource cannot be set while using Binding.Source.'
binding.Path = new PropertyPath(typeof(MyStaticClass).GetProperty(nameof(MyStaticClass.Left)));
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
this.SetBinding(Window.LeftProperty, binding);

...of course if you're setting Binding from the code remove any bindings in XAML.

…当然,如果要从代码中设置绑定,请删除XAML中的任何绑定。

OneWay (property changes from the source):

And if you'd need to update the target (i.e. the control's property, Window.Left in this case) on the source property changes, that can't be achieved with the static class (as per my comment above, you'd need the INotifyPropertyChanged implemented, so you could just use a wrapper class, implement INotifyPropertyChanged and wire that to a static property of your interest (providing you know how to track you static property's changes, i.e. this is more of a 'design' issue from this point on, I'd suggest redesigning and putting it all within one 'non-static' class).

如果您需要更新目标(即控件的属性,窗口)。左在这种情况下)的源属性更改,与静态类不能实现(按我上面的评论,你需要实现INotifyPropertyChanged,所以你可以使用一个包装器类,实现INotifyPropertyChanged线,你感兴趣的静态属性(提供你知道如何追踪你静态属性的变化,即这更多的是一种“设计”问题从这一点,我建议重新设计,并把这些都在一个非静态的类)。

#3


0  

You can use the newer x:Bind to do this simply using:

您可以使用较新的x:Bind吗?

<TextBlock Text="{x:Bind YourClassName.PropertyName}"/>