更新我已經實施了DataGrid
如下:數據網格不與的ObservableCollection
<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();
我錯過了什麼?
觀察集合只通知有關集合中的變化,不是在內部集合項目的變化。 – Silvermind
那是線程'UIThread'還是'NonUIThread'? –
這是一個NonUIThread – Abhishek