静态INotifyPropertyChanged事件不起作用
我有这个Base class
:
public abstract class WiresharkFile : BaseObservableObject, IDisposable
{
private string _fileName;
private int _packets;
private int _packetsSent;
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
public int Packets
{
get { return _packets; }
set { _packets = value; }
}
public int PacketsSent
{
get { return _packetsSent; }
set
{
_packetsSent = value;
OnPropertyChanged();
}
}
public void Dispose()
{
// Implemented insde inherit classes.
}
}
BaseObservableObject:
BaseObservableObject:
public class BaseObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
{
var propName = ((MemberExpression)raiser.Body).Member.Name;
OnPropertyChanged(propName);
}
protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
OnPropertyChanged(name);
return true;
}
return false;
}
}
我的收藏集:
public ObservableCollection<WiresharkFile> wiresharkFiles { get; set; }
因此,如您所见,我的Base class
的每个继承类都具有此PacketsSent
属性更改,因此在这种情况下,一切正常.
So as you can see avery inherit class from my Base class
have this PacketsSent
property change so in this case all works fine.
现在,我在WiresharkFile
(基类)内还有另一个静态属性:
Now i have another static property inside WiresharkFile
(base class):
private static volatile int _totalPacketsSent;
public static int TotalPacketsSent
{
get { return _totalPacketsSent; }
set
{
_totalPacketsSent = value;
OnStaticPropertyChanged();
}
}
因此在BaseObservableObject
内部,我创建了该成员:
So inside BaseObservableObject
i created this member:
public static event PropertyChangedEventHandler StaticPropertyChanged;
并且:
protected static void OnStaticPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = StaticPropertyChanged;
if (handler != null) handler(typeof(WiresharkFile), new PropertyChangedEventArgs(propertyName));
}
在XAML
内部,我想更新我的Label
:
And inside XAML
i want to update my Label
:
Content="{Binding Path=(my:WiresharkFile.TotalPacketsSent)}"
所以这不起作用,因此当前Label
通过后面的代码更新.
我做错什么了吗?
So this is not working so currently this Label
is updated via code behind.
As i doing something wrong ?
必须在类WiresharkFile
中声明静态属性更改事件(即,该类也声明了静态属性).如果在基类中声明,它将无法正常工作.
The static property changed event has to be declared in class WiresharkFile
(i.e. the class that also declares the static property). It won't work if it is declared in a base class.
public class WiresharkFile : BaseObservableObject
{
public static event PropertyChangedEventHandler StaticPropertyChanged;
private static void OnStaticPropertyChanged(string propertyName)
{
var handler = StaticPropertyChanged;
if (handler != null)
{
handler(null, new PropertyChangedEventArgs(propertyName));
}
}
// static property here
}