静态变量的 PropertyChangedEventHandler 未更改
PropertyChangedEventHandler for static variable not changing
我想知道如何用我的 static
属性 更新我的 UI
。
我的模型中有 2 个属性,其中之一是 static
:
public class Machine : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static event PropertyChangedEventHandler StaticPropertyChanged;
public string _name;
public static int _counter;
public void AddMachine(Machine machine)
{
_counter++;
}
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChange("Name");
}
}
public static int Counter
{
get { return _counter; }
set
{
_counter = value;
OnStaticlPropertyChanged("Counter");
}
}
public virtual void NotifyPropertyChange(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
public static void OnStaticlPropertyChanged(string propertyName)
{
var handler = StaticPropertyChanged;
if (handler != null)
StaticPropertyChanged(
typeof(Machine),
new PropertyChangedEventArgs(propertyName));
}
}
如您所见,我为我的 static
值创建了另一个 PropertyChangedEventHandler
。
另一个 属性(不是静态的 - 名称)工作正常。
我把我的 object 放在 collection:
public ObservableCollection<Machine> machines { get; set; }
而且我可以看到 Counter
正在更改但没有更新我的 UI
,这就是我尝试使用 TextBlock
更新我的 UI
的方式:
<TextBlock Name="tbStatusBar" Text="{Binding Source={x:Static my:Machine.Counter}}" />
所以我的问题是我做错了什么?
首先,您必须确保使用 WPF 4.5,因为 StaticPropertyChanged
事件机制不适用于旧版本。
然后,在 AddMachine
中,您应该更新 属性,而不是其支持字段:
public void AddMachine(Machine machine)
{
Counter++;
}
最后,您必须将绑定表达式从静态源更改为静态路径属性(注意括号):
<TextBlock Text="{Binding Path=(my:Machine.Counter)}" />
我想知道如何用我的 static
属性 更新我的 UI
。
我的模型中有 2 个属性,其中之一是 static
:
public class Machine : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static event PropertyChangedEventHandler StaticPropertyChanged;
public string _name;
public static int _counter;
public void AddMachine(Machine machine)
{
_counter++;
}
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChange("Name");
}
}
public static int Counter
{
get { return _counter; }
set
{
_counter = value;
OnStaticlPropertyChanged("Counter");
}
}
public virtual void NotifyPropertyChange(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
public static void OnStaticlPropertyChanged(string propertyName)
{
var handler = StaticPropertyChanged;
if (handler != null)
StaticPropertyChanged(
typeof(Machine),
new PropertyChangedEventArgs(propertyName));
}
}
如您所见,我为我的 static
值创建了另一个 PropertyChangedEventHandler
。
另一个 属性(不是静态的 - 名称)工作正常。
我把我的 object 放在 collection:
public ObservableCollection<Machine> machines { get; set; }
而且我可以看到 Counter
正在更改但没有更新我的 UI
,这就是我尝试使用 TextBlock
更新我的 UI
的方式:
<TextBlock Name="tbStatusBar" Text="{Binding Source={x:Static my:Machine.Counter}}" />
所以我的问题是我做错了什么?
首先,您必须确保使用 WPF 4.5,因为 StaticPropertyChanged
事件机制不适用于旧版本。
然后,在 AddMachine
中,您应该更新 属性,而不是其支持字段:
public void AddMachine(Machine machine)
{
Counter++;
}
最后,您必须将绑定表达式从静态源更改为静态路径属性(注意括号):
<TextBlock Text="{Binding Path=(my:Machine.Counter)}" />