资源更改时静态绑定不会更新

资源更改时静态绑定不会更新

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

问题描述

首先我想说我对绑定非常陌生.我已经在 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() 被调用时,静态类将获得新值,但是文本框的背景不会改变.我在这里做错了什么?正如我所说,我对此很陌生,我很感激外行人的解决方案.

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.

谢谢!

推荐答案

首先,你的 property 实际上不是一个属性,而是一个字段.最小的属性声明如下所示:

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

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

09-05 01:14