我不知道如何更新我的UI
与我的static
属性。 我在我的模型2个属性和其中一个static
:PropertyChangedEventHandler静态变量不变
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
。 其他属性(不是静态的 - 名称)工作正常。 我捧在收集我的对象:
public ObservableCollection<Machine> machines { get; set; }
,我可以看到Counter
正在发生变化,但没有更新我的UI
,我这是怎么尝试使用我的UI
TextBlock
更新:
<TextBlock Name="tbStatusBar" Text="{Binding Source={x:Static my:Machine.Counter}}" />
所以我的问题是我做错了什么?
您应该在AddMachine中用'Counter ++'替换'_counter ++'。 – Clemens
我已经试过了,这不是问题 – user979033
你实际使用WPF 4.5吗? StaticPropertyChanged事件机制不适用于较旧的版本。 – Clemens