资源更改时,静态绑定不会更新 [英] Static binding doesn't update when resource changes

查看:76
本文介绍了资源更改时,静态绑定不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先要说我是Binding的新手。我已经在WPF中做过一些事情,但是我从未使用过绑定,因为对于我来说,概念太难理解了。即使是我现在正在做的事情,也可以从我不完全理解的教程中得以解决。

I'd first like to say I'm very new to Binding.. I've done some things in WPF already but I never used binding because concept is a bit too hard to understand for me right of the bat. Even this what I'm doing now is something i managed to salvage from a tutorial that I didn't fully understand.

在我的应用程序中,我有一个带有静态变量的静态类属性,并且有一个更改这些静态属性的静态方法。

In my application I have a static class with static properties and there's a static method that changes those static properties.

示例:

public static class AppStyle
{
    public static SolidColorBrush property = Brushes.Red;


    public static void ChangeTheme()
    {
        property = Brushes.Blue;
    }
}

在XAML中,我有一个具有背景的控件绑定到该值。我什至正确地声明了命名空间。

Inside the XAML I have a control that has it's background binded to this value. I even declared the namespace properly.

...
    xmlns:style="clr-namespace:CorrectNamespace;assembly=RightAssembly"
...
<TextBox x:Name="TXT_PN" 
     Background="{Binding Source={x:Static style:AppStyle.property}}"          
     TextChanged="TXT_PN_TextChanged" 
     Text="Text"/>

当应用程序加载时,它将加载正确的设置(红色),但是当事情改变时,ChangeTheme( )被调用时,静态类将获得新值,但是文本框的Background不会更改。
我在这里做错了什么?正如我所说的,我对此很陌生,很感谢以外行的方式提出的解决方案。

When the application loads it will load the correct setting (Red color) however when things change and ChangeTheme() is called, the static class will get the new value, however the textbox's Background will not change. What am I doing wrong here? As I said, I'm very new to this and I would appreciate the solution in laymen's terms.

谢谢!

推荐答案

首先,您的属性实际上不是属性,而是字段。最小的属性声明如下所示:

First of all, your property is actually not a property, but a field. A minimal property declaration would look like this:

public static SolidColorBrush Property { get; set; }

请注意,该名称以大写字母开头,这是C#中广泛接受的编码约定

Please note the name is starting with an uppercase letter, which is a widely accepted coding convention in C#.

由于您还希望在属性值发生更改时都发出更改通知,因此需要声明一个属性更改的事件(对于非静态属性,该事件通常是通过实现INotifyPropertyChanged接口来完成的。)

Because you also want to have a change notification fired whenever the value of the property changes, you need to declare a property-changed event (which for non-static properties is usually done by implementing the INotifyPropertyChanged interface).

对于静态属性,WPF 4.5(或4.0?)中有一种新机制,您可以在其中写入已更改的静态属性。事件和属性声明如下:

For static properties there is a new mechanism in WPF 4.5 (or 4.0?), where you can write a static property changed event and property declaration like this:

public static class AppStyle
{
    public static event PropertyChangedEventHandler StaticPropertyChanged;

    private static void OnStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
    }

    private static SolidColorBrush property = Brushes.Red; // backing field

    public static SolidColorBrush Property
    {
        get { return property; }
        set
        {
            property = value;
            OnStaticPropertyChanged("Property");
        }
    }

    public static void ChangeTheme()
    {
        Property = Brushes.Blue;
    }
}

与静态属性的绑定将使用括号中的属性路径:

The binding to a static property would be written with the property path in parentheses:

Background="{Binding Path=(style:AppStyle.Property)}"          

这篇关于资源更改时,静态绑定不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆