虽然我实现了INotifyPropertyChanged,但ObservableCollection没有更新

问题描述 投票:0回答:1

我需要在DataGrid中显示从外部源接收的一组位状态。为此,我使用像这样的ObservableCollection

public class UpdateIO : INotifyPropertyChanged
public static ObservableCollection<IObitDetails> PlcCommonOutputs = new ObservableCollection<IObitDetails>();

我的IObitDetails类是

    public class IObitDetails : INotifyPropertyChanged
    {
        bool _bitValue;
        public string BitGroup { get; set; }
        public string BitText { get; set; }
        public short CIOaddress { get; set; }
        public short BitPosition { get; set; }
        public bool BitValue
        {
            get { return _bitValue; }
            set { SetField(ref _bitValue, value); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetField<T>(ref T field, T value,
        [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value))
                return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        public IObitDetails(string bg, bool bv, string bt, short adr, short pos)
        {
            BitGroup = bg;
            BitValue = bv;
            BitText = bt;
            CIOaddress = adr;
            BitPosition = pos;
        }
    }

接下来我创建了这个集合

PlcCommonOutputs.Add(new IObitDetails("CO", oCommonOpLampRed,"Op. Red", 0x0001, obitCommonOpLampRed));
PlcCommonOutputs.Add(new IObitDetails("CO", oCommonOpLampGreen,"Op. Green", 0x0001, obitCommonOpLampGreen));
PlcCommonOutputs.Add(new IObitDetails("CO", oCommonMuteA,"MuteA", 0x0001, obitCommonMuteA));
PlcCommonOutputs.Add(new IObitDetails("CO", oCommonMuteB,"MuteB", 0x0001, obitCommonMuteB));
...

这里BitPosition声明为:

private const short obitCommonOpLampRed = 0;
private const short obitCommonOpLampGreen = 1;
private const short obitCommonMuteA = 2;
private const short obitCommonMuteB = 3;
...

每个位都是一个属性,当我在oCommonOutputsPost中读取(在轮询线程中)端口并存储时,该属性被修改:

        private short _oCommonOutputsPort;
        public short oCommonOutputsPort
        {
            get { return _oCommonOutputsPort; }
            set
            {
                SetField(ref _oCommonOutputsPort, value);
                oCommonOpLampRed = (oCommonOutputsPort & (1 << obitCommonOpLampRed)) != 0;
                oCommonOpLampGreen = (oCommonOutputsPort & (1 << obitCommonOpLampGreen)) != 0;
                oCommonMuteA = (oCommonOutputsPort & (1 << obitCommonMuteA)) != 0;
                oCommonMuteB = (oCommonOutputsPort & (1 << obitCommonMuteB)) != 0;
            }
        }

        private bool _oCommonOpLampRed;
        public bool oCommonOpLampRed
        {
            get { return _oCommonOpLampRed; }
            set { SetField(ref _oCommonOpLampRed, value); }
        }
        private bool _oCommonOpLampGreen;
        public bool oCommonOpLampGreen
        {
            get { return _oCommonOpLampGreen; }
            set { SetField(ref _oCommonOpLampGreen, value); }
            }
        }
        private bool _oCommonMuteA;
        public bool oCommonMuteA
        {
            get { return _oCommonMuteA; }
            set { SetField(ref _oCommonMuteA, value); }
        }
        private bool _oCommonMuteB;
        public bool oCommonMuteB
        {
            get { return _oCommonMuteB; }
            set { SetField(ref _oCommonMuteB, value); }
        }
...

每当我读取一个新的端口值时,这些位都被置位/复位(我看到位属性被正确修改)。但是ObservableCollection(PlcCommonOutputs)没有改变。如果我在调试器中手动更改集合中相应的BitValue,则可以在UI中看到它们(因此我不包含任何xaml)。有没有办法在不增加代码复杂性的情况下完成这项工作?

c# observablecollection
1个回答
0
投票

一个简单的解决方法是在你的setter中为你想要更新的值提高propertychanged。一个例子是:

private bool _oCommonMuteA;
public bool oCommonMuteA
{
    get { return _oCommonMuteA; }
    set 
    { 
        SetField(ref _oCommonMuteA, value); 
        OnPropertyChanged(nameof(oCommonMuteA));
    }
 }
© www.soinside.com 2019 - 2024. All rights reserved.