2012-03-20 25 views
6

Tôi đã có một biến mà không phải là tĩnh và INotifyPropertyChanged thực hiện thành công. Sau đó, tôi đã cố gắng để làm cho nó toàn cầu, do đó biến nó thành một biến tĩnh. Nhưng lần này, INotifyPropertyChanged không hoạt động. Bất kì giải pháp nào?INotifyPropertyChanged cho biến tĩnh

+0

thể trùng lặp của [Ràng buộc về tài sản tĩnh] (http://stackoverflow.com/questions/936304/binding-to-static-property) –

Trả lời

21

INotifyPropertyChanged hoạt động trên các thuộc tính mẫu. Một giải pháp là sử dụng mẫu đơn và giữ INotifyPropertyChanged, cách khác là sử dụng sự kiện của riêng bạn để thông báo cho người nghe.

Singleton dụ

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

EDIT: Trong WPF 4.5, họ giới thiệu bất động sản thay đổi cơ khí cho các thuộc tính tĩnh:

Bạn có thể sử dụng thuộc tính tĩnh như nguồn gốc của một ràng buộc dữ liệu. Công cụ gắn kết dữ liệu nhận ra khi giá trị của thuộc tính thay đổi nếu sự kiện tĩnh được nâng lên. Ví dụ: nếu lớp SomeClass định nghĩa thuộc tính tĩnh được gọi là MyProperty, SomeClass có thể xác định sự kiện tĩnh được tăng khi giá trị MyProperty thay đổi. Sự kiện tĩnh có thể sử dụng một trong các chữ ký sau.

public static event EventHandler MyPropertyChanged; 
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged; 
+3

+1 cho đề xuất singleton –

+0

Một ví dụ nhỏ? – Shibli

+0

@Shilbi Một ví dụ nhỏ về cái gì? – chopikadze

5

dụ Rất tốt, tôi sử dụng nó cho một số cài đặt chung trong ứng dụng, khi tôi muốn để ràng buộc một số tài sản trực tuyến với các thành phần

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

Set thuộc tính khởi động:

DataGridClass.Instance.DataGridFontSize = 14(or read from xml) 

Liên kết tài khoản này với các thành phần

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> 

Khi bạn thay đổi giá trị này ở đâu đó trong ứng dụng như "Preferences" -> DataGrid FontSize - để nó tự động cập nhật tài sản này cho các ràng buộc với 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}"/> 
+0

Cảm ơn rất nhiều vì ví dụ này, đó chính xác là những gì tôi cần. Nó hoạt động một điều trị! – RobHurd