辅助静态绑定失败

时间:2018-09-21 08:07:26

标签: c# wpf binding

以下是静态绑定的一个很好的示例:https://www.c-sharpcorner.com/UploadFile/mahesh/binding-static-properties-in-wpf-4-5/

我正在尝试添加第二个,但是它没有更新。仅第一个,ApplicationTitle被更新。

xaml代码:

<Grid>
    <StackPanel Orientation="Vertical">
        <TextBox x:Name="TxtApplicationTitle"  Text="{Binding Path=(local:AppSettingApproach1.ApplicationTitle), Mode=TwoWay,   UpdateSourceTrigger=PropertyChanged}" />
        <TextBox x:Name="TxtApplicationTitle2"  Text="{Binding Path=(local:AppSettingApproach1.ApplicationTitle2), Mode=TwoWay,   UpdateSourceTrigger=PropertyChanged}" />
    </StackPanel>
</Grid>

我的课:

class AppSettingApproach1
{
    public static event EventHandler ApplicationTitleChanged;
    public static event EventHandler ApplicationTitleChanged2;

    private static string _applicationTitle;
    private static string _applicationTitle2;

    public static string ApplicationTitle
    {
        get { return _applicationTitle; }
        set
        {
            if (value != _applicationTitle)
            {
                _applicationTitle = value;
                if (ApplicationTitleChanged != null)
                    ApplicationTitleChanged(null, EventArgs.Empty);
            }
        }
    }

    public static string ApplicationTitle2
    {
        get { return _applicationTitle2; }
        set
        {
            if (value != _applicationTitle2)
            {
                _applicationTitle2 = value;
                Console.Beep();
                if (ApplicationTitleChanged2 != null)
                    ApplicationTitleChanged2(null, EventArgs.Empty);
            }
        }
    }

}

启动类

public MainWindow()
{
    InitializeComponent();
    AppSettingApproach1.ApplicationTitle = @"Hello WPF, this my first Approach of static binding.";
    AppSettingApproach1.ApplicationTitle2 = @"Hello WPF, this my SECOND Approach of static binding.";
}

1 个答案:

答案 0 :(得分:0)

静态属性更改事件的命名约定为<PropertyName>Changed,所以

public static event EventHandler ApplicationTitleChanged2;

是错误的名称。一定是

public static event EventHandler ApplicationTitle2Changed;