DataGrid不使用ObservableCollection更新

时间:2014-06-13 12:37:43

标签: c# wpf datagrid

我已经实施了DataGrid,如下所示:

<DataGrid Name="grdSignals" Grid.Column="1" ItemsSource="{Binding}"  AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Signal Name" Binding="{Binding SignalName}"/>
        <DataGridTextColumn Header="Value" Binding="{Binding SignalValue}" />
    </DataGrid.Columns>
</DataGrid>

以下是Class,其中我实现了ObservableCollection中使用的属性:

namespace Test
{
    public partial class MyMain : UserControl
    {        
        public ObservableCollection<Signals> ocSignalNames;
        Signals time, mode;

        public MyMain()
        {
            InitializeComponent();    
            ocSignalNames = new ObservableCollection<Signals>();            
            time = new Signals() { SignalName = "Time", SignalValue = "" };
            mode = new Signals() { SignalName = "Mode", SignalValue = "" };
            ocSignalNames.Add(time);
            ocSignalNames.Add(mode);
            grdSignals.DataContext = ocSignalNames;
        }
    }

    public class Signals : INotifyPropertyChanged
    {
        string _signalName, _signalValue;
        public string SignalName
        {
            get
            {
                return _signalName;
            }
            set
            {
                _signalName = value;
            }
        }

        public string SignalValue
        {
            get
            {
                return _signalValue;
            }
            set
            {
                _signalValue = value;
                OnPropertyChanged(SignalValue);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

调试后,我发现ocSignalNames在项目发生变化时会更新。但这些变化并没有反映在DataGrid中。我正在更新这样的项目中的项目:

time.SignalValue = iTest1.ToString();
mode.SignalValue = iTest2.ToString();

我错过了什么?

2 个答案:

答案 0 :(得分:0)

您缺少正确的NotifyChanged处理程序:

    public string SignalName
    {
        get
        {
            return _signalName;
        }
        set
        {
            _signalName = value;
            OnPropertyChanged("SignalName"); //Added
        }
    }

    public string SignalValue
    {
        get
        {
            return _signalValue;
        }
        set
        {
            _signalValue = value;
            OnPropertyChanged("SignalValue"); //NOTE: quotation marks added
        }
    }

答案 1 :(得分:0)

你必须使用下面提到的代码。

 string _signalName, _signalValue;
    public string SignalName
    {
        get
        {
            return _signalName;
        }
        set
        {
            _signalName = value;
            OnPropertyChanged("SignalName");
        }
    }

    public string SignalValue
    {
        get
        {
            return _signalValue;
        }
        set
        {
            _signalValue = value;
            OnPropertyChanged(SignalValue);
        }
    }

你的XAML看起来像

 <DataGrid Name="grdSignals" Grid.Column="1" ItemsSource="{Binding}"  AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Signal Name" Binding="{Binding SignalName, Mode=TwoWay}"/>
            <DataGridTextColumn Header="Value" Binding="{Binding SignalValue, Mode=TwoWay}" />
        </DataGrid.Columns>
    </DataGrid>

O / P

enter image description here