class generatedMessage
{
public CanMessage cmsg { set; get; }
public int cycleTime { set; get; }
public generatedMessage(){}
}
public class CanMessage
{
public byte[] data { set; get; }
public ushort dlc { set; get; }
public ushort flags { set; get; }
public uint id { set; get; }
public ulong res1 { set; get; }
public ulong res2 { set; get; }
}
你看到generatedMessage类有一个CanMessage类的对象,我之前用CanMessage类完成了这个,如下所示
TraceTable.Items.Add /*- at TraceTable is a datagrid -*/
(
new CanMessage()
{
data = msg.data,
dlc = msg.dlc,
id = msg.id,
flags = msg.flags,
res1 = msg.res1,
res2 = msg.res2
}
);
//xml file below
<DataGrid x:Name="TraceTable" Grid.Column="0" DockPanel.Dock="Top" SelectionMode="Extended" AutoGenerateColumns="False" SelectionUnit="FullRow" Margin="0,45,4,0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=data}" Header="Data" IsReadOnly="true"/>
<DataGridTextColumn Binding="{Binding Path=dlc}" Header="DLC" IsReadOnly="true"/>
<DataGridTextColumn Binding="{Binding Path=id}" Header="ID" IsReadOnly="true"/>
<DataGridTextColumn Binding="{Binding Path=flag}" Header="Flag" IsReadOnly="true"/>
<DataGridTextColumn Binding="{Binding Path=res1}" Header="Res1" IsReadOnly="true"/>
<DataGridTextColumn Binding="{Binding Path=res2}" Header="Res2" IsReadOnly="true"/>
</DataGrid.Columns>
</DataGrid>
所以我的问题是如何使用generatedMessage类来实现它?我希望datagrid能够查看所有canMessage属性+循环时间(一个包含7列的数据网格)
答案 0 :(得分:1)
如果我正确理解了问题,那么你应该添加“generatedMessage”类型的项目并进行绑定,如:
Binding={Binding cycleTime}
Binding={Binding cmsg.dlc}
...
etc.
通常,您不应手动将项目添加到DataGrid。最好的方法:在ViewModel中创建ObservableCollection并在DataGrid中设置属性“ItemsSource”:
ItemsSource="{Binding Collection}"
然后将项目添加到此集合,而不是DataGrid。 为了更好地理解该主题,您可以阅读有关MVVM模式的内容; - )。