绑定到静态类属性

发布于 2024-09-26 00:02:16 字数 63 浏览 1 评论 0原文

我想将文本块文本绑定到静态类的属性。每当静态类的属性值发生更改时,它都应该反映到其他窗口或自定义控件上的文本块。

I want to bind a textblock text to a property of a static class. Whenever the property value of the static class changes, it should reflect to the textblock which is on the other window or custom control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猥︴琐丶欲为 2024-10-03 00:02:16

您可以使用 x:Static 绑定到静态类上的任何属性标记扩展,但如果您不实现任何更改跟踪,则可能会导致刷新错误!

<TextBlock Text="{Binding Source={x:Static sys:Environment.MachineName}}" />

You can bind to ANY property on a static class using the x:Static markup extension but if thy do not implement any change tracking, it might cause errors on the refresh!

<TextBlock Text="{Binding Source={x:Static sys:Environment.MachineName}}" />
少女情怀诗 2024-10-03 00:02:16

对于那些使用嵌套静态类来组织/分隔常量的人。如果您需要绑定到嵌套静态类,似乎您需要使用加号(+)运算符而不是点(.)运算符来访问嵌套类:

{Binding Source={x:Static namespace:StaticClass+NestedStaticClasses.StaticVar}}

示例:

public static class StaticClass
    {
        public static class NestedStaticClasses
        {
            public static readonly int StaticVar= 0;

        }
    }

For those who use nested static classes to organize/seperate constants. If you need to Bind into nested static classes, It seems you need to use a plus (+) operator instead of the dot (.) operator to access the nested class:

{Binding Source={x:Static namespace:StaticClass+NestedStaticClasses.StaticVar}}

Example:

public static class StaticClass
    {
        public static class NestedStaticClasses
        {
            public static readonly int StaticVar= 0;

        }
    }
神魇的王 2024-10-03 00:02:16

这对我有用:

Text="{Binding Source={x:Static MyNamespace:MyStaticClass.MyProperty}, Mode=OneWay}"

没有 Mode=OneWay 我得到了一个例外。

This has worked for me:

Text="{Binding Source={x:Static MyNamespace:MyStaticClass.MyProperty}, Mode=OneWay}"

Without Mode=OneWay I got an exception.

相权↑美人 2024-10-03 00:02:16

这对我有用!

当你有像这样带有静态属性的静态类时,

 namespace App.Classes
 {
     public static class AppData
     {
         private static ConfigModel _configModel;
         public static ConfigModel Configuration
         {
            get { return _configModel; }
            set { _configModel = value; }
         }
     }

     public class ConfigModel : INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;

          private bool _text = true;
          public bool Text
          {
               get { return _text ; }
               set { 
                     _text = value; 
                     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text"));
               }
          }
      }
}

你可以像这样将它绑定到xaml。

xmlns:c="clr-namespace:App.Classes"

<TextBlock Text="{Binding Path=Text, Source={x:Static c:AppData.Configuration}}"/>

It worked for me!

When you have static class with static property like this

 namespace App.Classes
 {
     public static class AppData
     {
         private static ConfigModel _configModel;
         public static ConfigModel Configuration
         {
            get { return _configModel; }
            set { _configModel = value; }
         }
     }

     public class ConfigModel : INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;

          private bool _text = true;
          public bool Text
          {
               get { return _text ; }
               set { 
                     _text = value; 
                     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text"));
               }
          }
      }
}

You can bind it to xaml like this.

xmlns:c="clr-namespace:App.Classes"

<TextBlock Text="{Binding Path=Text, Source={x:Static c:AppData.Configuration}}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文