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
Trả lời
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;
+1 cho đề xuất singleton –
Một ví dụ nhỏ? – Shibli
@Shilbi Một ví dụ nhỏ về cái gì? – chopikadze
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}"/>
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
- 1. INotifyPropertyChanged problem
- 2. INotifyPropertyChanged trong phân lớp
- 3. INotifyPropertyChanged vs. DependencyProperty
- 4. INotifyPropertyChanged và Threading
- 5. biến tĩnh trong ruby
- 6. Biến nhiễu tĩnh ASP.net?
- 7. biến tĩnh trong c
- 8. Con trỏ biến tĩnh?
- 9. Biến đổi tĩnh C++
- 10. Biến tĩnh trong Tcl
- 11. Biến tĩnh trong C#
- 12. biến tĩnh trong c
- 13. Biến tĩnh trong C++
- 14. Lớp/biến tĩnh Java
- 15. Biến tĩnh so với Biến động
- 16. INotifyPropertyChanged vs IObservable - RxExtensions, cái nào cho mẫu quan sát
- 17. Đăng ký INotifyPropertyChanged cho các đối tượng lồng nhau (con)
- 18. C# sử dụng biến tĩnh làm tham số cho DeploymentItem
- 19. Sử dụng biến tĩnh cho dữ liệu bộ nhớ cache
- 20. API Python C++: "Biến tĩnh" cho đối tượng kiểu
- 21. __getattr__ cho các biến tĩnh/lớp trong python
- 22. Biến cục bộ tĩnh cho các phương thức trong Ruby?
- 23. Tại sao các biến tĩnh không cho phép đệ quy?
- 24. Cập nhật biến tĩnh Java
- 25. Trường tĩnh và Biến số
- 26. Biến tĩnh mất giá trị
- 27. Công giá trị biến tĩnh
- 28. Truy cập các biến tĩnh
- 29. DotNet: Biến tĩnh trong DLL
- 30. Tiếp cận biến tĩnh từ phương pháp tĩnh
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) –