我有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:
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
中的avery继承类有PacketsSent
属性更改所以在这种情况下一切正常。
现在我在WiresharkFile
(基类)中有另一个静态属性:
private static volatile int _totalPacketsSent;
public static int TotalPacketsSent
{
get { return _totalPacketsSent; }
set
{
_totalPacketsSent = value;
OnStaticPropertyChanged();
}
}
所以在BaseObservableObject
内我创建了这个成员:
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
:
Content="{Binding Path=(my:WiresharkFile.TotalPacketsSent)}"
所以这不起作用,所以目前通过后面的代码更新Label
。
我做错了什么?
答案 0 :(得分:1)
必须在类WiresharkFile
中声明static属性changed事件(即也声明静态属性的类)。如果在基类中声明它将无法工作。
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
}
答案 1 :(得分:0)
由于您的创建PropertyChangedEventHandler不是真正的INotifyPropertyChanged接口的一部分,因此绑定不会识别抛出通知事件,因此它不会从TotalPacketsSent中提取任何新值。要使绑定刷新本身尝试这样的事情: 放入WiresharkFile类:
private static WiresharkFile This;
public WiresharkFile()
{
This = this;
}
private static volatile int _totalPacketsSent;
public static int TotalPacketsSent
{
get { return _totalPacketsSent; }
set
{
_totalPacketsSent = value;
OnStaticPropertyChanged(This);
}
}
- 处理程序代码:
protected static void OnStaticPropertyChanged(object sender, [CallerMemberName]string propertyName = null)
{
var baseObservable = sender as BaseObservableObject;
if(baseObservable == null) return;
baseObservable.OnPropertyChanged(propertyName);
}
在我看来,你在BaseObservableObject类中不需要PropertyChangedEventHandler事件。
的问候,