通知基类
public class NotifyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected void UpdateProper<T>(ref T properValue, T newValue, [CallerMemberName] string properName = "",
[CallerLineNumber] int line = 0, [CallerFilePath] string file = "")
{
if (object.Equals(properValue, newValue))
return;
properValue = newValue;
RaisePropertyChanged(properName);
}
protected void UpdateProper<T>(ref T properValue, T newValue,string properName)
{
if (object.Equals(properValue, newValue))
return;
properValue = newValue;
RaisePropertyChanged(properName);
}
2、数据
namespace ChassisByWireSystem.ViewModels
{
public class CandataInfo : Common.NotifyBase
{
uint ordernumber;
public uint 序号
{
get => ordernumber;
set { UpdateProper(ref ordernumber, value); }
}
string direction;
public string 方向
{
get => direction;
set { UpdateProper(ref direction, value); }
}
string time;
public string 时间
{
get => time;
set { UpdateProper(ref time, value); }
}
string frameid;
public string 帧ID
{
get => frameid;
set { UpdateProper(ref frameid, value); }
}
string frameformat;
public string 帧格式
{
get => frameformat;
set { UpdateProper(ref frameformat, value); }
}
string frametype;
public string 帧类型
{
get => frametype;
set { UpdateProper(ref frametype, value); }
}
string dlc;
public string DLC
{
get => dlc;
set { UpdateProper(ref dlc, value); }
}
string candata;
public string 数据
{
get => candata;
set { UpdateProper(ref candata, value); }
}
public CandataInfo() { }
public CandataInfo(uint number, string dir, string time, string fid, string fformat, string ftype, string datalenth, string candata)
{
序号 = number;
方向 = dir;
时间 = time;
帧ID = fid;
帧格式 = fformat;
帧类型 = ftype;
DLC = datalenth;
数据 = candata;
}
}
public class CanData : System.Collections.ObjectModel.ObservableCollection<CandataInfo> { }
}
3、XAML文件引用
<Window.Resources>
<viewmodels:CanData x:Key="DataSend">
</viewmodels:CanData>
<viewmodels:CanData x:Key="DataRecv">
</viewmodels:CanData>
</Window.Resources>
<DataGrid x:Name="datagrid1" EnableRowVirtualization="True" FontSize="12" EnableColumnVirtualization="False"
ItemsSource="{Binding Source={StaticResource DataSend}}" IsReadOnly="True">
</DataGrid>
<DataGrid Grid.Column="1" x:Name="datagrid2" EnableRowVirtualization="True" FontSize="12" EnableColumnVirtualization="False"
ItemsSource="{Binding Source={StaticResource DataRecv}}" IsReadOnly="True">
</DataGrid>
4、代码样例
new Thread(o =>
{
while (true)
{
Dispatcher.Invoke(() =>
{
if (frameDir.Equals("发送"))
{
CanData datasend = (CanData)this.FindResource("DataSend");
datasend.Add(new CandataInfo((uint)datagrid1.Items.Count, frameDir, time,
sframeid, framformat, frametype, frameDlc, sdata));
datagrid1.ScrollIntoView(datagrid1.Items[datagrid1.Items.Count - 1]);
}
else
{
CanData dataRecv = (CanData)this.FindResource("DataRecv");
dataRecv.Add(new CandataInfo((uint)datagrid2.Items.Count, frameDir, time,
sframeid, framformat, frametype, frameDlc, sdata));
datagrid2.ScrollIntoView(datagrid2.Items[datagrid2.Items.Count - 1]);
}
});
Thread.sleep(1);
}
})
{ IsBackground = true }.Start();
5、
image.png