C# 已更改静态变量的INotifyProperty

C# 已更改静态变量的INotifyProperty,c#,wpf,static,inotifypropertychanged,C#,Wpf,Static,Inotifypropertychanged,我有一个非静态的变量,成功地实现了INotifyPropertyChanged。然后我试着使它全局化,所以把它变成了一个静态变量。但这一次,INotifyPropertyChanged不起作用。任何解决方案?INotifyPropertyChanged对实例属性有效。一种解决方案是使用单例模式并保持INotifyPropertyChanged,另一种是使用您自己的事件通知侦听器 单例 public sealed class MyClass: INotifyPropertyChanged {

我有一个非静态的变量,成功地实现了INotifyPropertyChanged。然后我试着使它全局化,所以把它变成了一个静态变量。但这一次,INotifyPropertyChanged不起作用。任何解决方案?

INotifyPropertyChanged
对实例属性有效。一种解决方案是使用单例模式并保持
INotifyPropertyChanged
,另一种是使用您自己的事件通知侦听器

单例

public sealed class MyClass: INotifyPropertyChanged
{
   private static readonly MyClass instance = new MyClass();
   private MyClass() {}

   public static MyClass Instance
   {
      get 
      {
         return instance; 
      }
   }

   // notifying property
   private string privMyProp;
   public string MyProp
   {
       get { return this.privMyProp; }

       set
       {
           if (value != this.privMyProp)
           {
               this.privMyProp = value;
               NotifyPropertyChanged("MyProp");
           }
       }
   }


   // INotifyPropertyChanged implementation
   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged(String info)
   {
       var handler = PropertyChanged;
       if (handler != null)
       {
           handler(this, new PropertyChangedEventArgs(info));
       }
   }
}
编辑:在WPF 4.5中,他们为静态属性引入了属性更改机制:

DataGridClass.Instance.DataGridFontSize = 14(or read from xml)
您可以使用静态属性作为数据绑定的源。这个 数据绑定引擎可以识别当属性值发生更改时 引发静态事件。例如,如果类SomeClass定义了 静态属性称为MyProperty,SomeClass可以定义静态事件 当MyProperty的值更改时引发的。静态事件 可以使用以下任一签名

公共静态事件事件处理程序MyPropertyChanged;
公共静态事件EventHandler StaticPropertyChanged;

这是一个很好的例子,当我想将某些属性在线绑定到组件时,我将其用于应用程序中的一些常规设置

    public sealed class DataGridClass:INotifyPropertyChanged
{
      private static readonly DataGridClass instance = new DataGridClass();
      private DataGridClass() { }

      public static DataGridClass Instance
       {
          get 
          {
             return instance; 
          }
       }

    private int _DataGridFontSize {get;set;}

    public int DataGridFontSize
    {
        get { return _DataGridFontSize; }
        set { _DataGridFontSize = value;
            RaisePropertyChanged("DataGridFontSize");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
设置启动属性:

DataGridClass.Instance.DataGridFontSize = 14(or read from xml)
将此绑定到组件属性

xmlns:static="clr-namespace:MyProject.Static"
<extgrid:ExtendedDataGrid x:Name="testGrid"  ClipboardCopyMode="IncludeHeader" AutoGenerateColumns="False">
<extgrid:ExtendedDataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="FontSize" 
        Value="{Binding Source={x:Static static:DataGridClass.Instance},
        Path=DataGridFontSize, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
    </Style>
</extgrid:ExtendedDataGrid.Resources>
xmlns:static=“clr命名空间:MyProject.static”

当您在应用程序中的某个位置更改此值(如“首选项”->DataGrid FontSize)时,它会自动为使用UpdateSourceTrigger的绑定更新此属性

    private void comboBoxFontSize_DropDownClosed(object sender, EventArgs e)
    {
        DataGridClass.Instance.DataGridFontSize = Convert.ToInt32(comboBoxFontSize.Text);
    }


   <ComboBox Grid.Column="1" Grid.Row="0" Height="21" Width="75" Name="comboBoxFontSize" HorizontalAlignment="Left" 
                              VerticalAlignment="Center"  DropDownClosed="comboBoxFontSize_DropDownClosed" 
                              ItemsSource="{Binding Source={x:Static commands:ConstClass.ListOfFontSize},Mode=OneWay}" 
                              SelectedItem="{Binding Source={x:Static static:DataGridClass.Instance},Path=DataGridFontSize, 
                        Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
private void ComboxFontSize\u DropDownClosed(对象发送方,事件参数e)
{
DataGridClass.Instance.DataGridFontSize=Convert.ToInt32(comboBoxFontSize.Text);
}

@Shilbi什么的一个小例子?我在哪里可以阅读更多关于WPF 4.5和静态属性绑定的信息?@agafadam链接都已关闭,但我在web archive.org上找到了arvchived页面。可以找到一个很好的工作示例,
publicstaticeventpropertychangedventhandler StaticPropertyChanged
不能被继承,因此应该在调用它的类中。非常感谢这个示例,这正是我所需要的。这是一种享受!小心,我刚刚调试了一个与绑定到x:Static singleton相关的内存泄漏。现在正在试验上面的.NET4.5方法