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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15854708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:10:13  来源:igfitidea点击:

How can I bind a xaml property to a static variable in another class?

c#wpfxaml

提问by a7madx7

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.

我知道这可能很愚蠢,但我刚从 Win-forms 转移过来,感觉有点失落。

here is what I mean:

这就是我的意思:

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

采纳答案by Rohit Vats

First of all you can't bind to variable. You can bind only to propertiesfrom XAML. For binding to static property you can do in this way (say you want to bind Textproperty of TextBlock) -

首先,你不能绑定到variable. 您只能properties从 XAML绑定到。要绑定到静态属性,您可以通过这种方式进行(假设您要绑定 的Text属性TextBlock)-

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

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

这里local是其中的命名空间,你需要这样的XAML文件中声明你上面的类所在-

xmlns:local="clr-namespace:YourNameSpace"

回答by NSGaga-mostly-inactive

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 myin the XAML like xmlns:my="clr-namespace:MyNamespace"

确保包含namespace- 即my在 XAML 中定义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)

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



OneTimebinding:

OneTime捆绑:

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

你可以使用textBlock.Text = MyStaticClass.Left(小心你放置它的位置,post-init)

TwoWay(or OneWayToSource) binding:

TwoWay(或OneWayToSource)绑定:

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

OneWay(来自源的属性更改):

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 INotifyPropertyChangedimplemented, so you could just use a wrapper class, implement INotifyPropertyChangedand 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).

如果您需要在源属性更改时更新目标(即控件的属性,在这种情况下为 Window.Left),则无法通过静态类实现(根据我上面的评论,您需要已INotifyPropertyChanged实现,因此您可以只使用包装类,实现INotifyPropertyChanged并将其连接到您感兴趣的静态属性(前提是您知道如何跟踪静态属性的更改,即从这一点开始,这更像是一个“设计”问题,我建议重新设计并将其全部放在一个“非静态”类中)。

回答by slayernoah

You can use the newer x:Bindto do this simply using:

您可以使用较新的x:Bind来执行此操作,只需使用:

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