C# 将类中的数据和类列表中的数据绑定到同一WPF DataGrid
我对WPF比较陌生,我找不到任何解决当前问题的方法,所以我在这里尝试。 我有一个名为ToolType的类,它有两个参数(名称、id、模型),还有一个工具列表(id、RFID、shelfID)。 在我的一个观点中,我需要展示C# 将类中的数据和类列表中的数据绑定到同一WPF DataGrid,c#,list,wpfdatagrid,C#,List,Wpfdatagrid,我对WPF比较陌生,我找不到任何解决当前问题的方法,所以我在这里尝试。 我有一个名为ToolType的类,它有两个参数(名称、id、模型),还有一个工具列表(id、RFID、shelfID)。 在我的一个观点中,我需要展示 | ID | RFID | Name | Model | ShelfID | 作为DataGrid中的列,所以是“父”类和“子”类的混合,但我只是不知道如何去做——除了制作一个包含信息的全新字符串数组之外。 有人知道怎么做吗 代码: 类工具类型 { 公共字符串I
| ID | RFID | Name | Model | ShelfID |
作为DataGrid中的列,所以是“父”类和“子”类的混合,但我只是不知道如何去做——除了制作一个包含信息的全新字符串数组之外。
有人知道怎么做吗
代码:
类工具类型
{
公共字符串ID{get;set;}
公共字符串名称{get;set;}
公共字符串模型{get;set;}
公共整数间隔{get;set;}
公共列表工具列表=新列表();
...
}
DataGrid的WPF:
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="40,43,0,0" Name="dataGrid4" VerticalAlignment="Top" Width="305">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" Header="ID" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding RFID}" Header="RFID" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding name}" Header="Type" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding model}" Header="Model" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding nextMaintenance}" Header="Dato" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding space}" Header="Plads" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
如何按工具类型对工具进行分组:在您尝试执行此操作的地方提供代码总是很有用的,因此请这样做。对于数据网格
,您的数据上下文
是什么?如何设置?在代码中设置为:dataGrid4.ItemsSource=Controller.Instance.toolTypes;toolTypes是ToolTypeClassi的列表,我仍然需要创建一个新列表来显示数据——我的目标是只使用ToolType类及其工具列表。但是我可能需要在link中实现排序功能,以便将所有内容放入一个数据网格中。您需要创建一个新列表,但是您可以使用您的工具类型作为每个工具列表的标题(在分组时)…所以我需要创建一个包含两个类的数据的新列表?如果这是唯一的方法,那么寻找不同的方法就没有意义了。
class ToolType
{
public string ID { get; set; }
public string name { get; set; }
public string model { get; set; }
public int interval { get; set; }
public List<Tool> toolsList = new List<Tool>();
...
}
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="40,43,0,0" Name="dataGrid4" VerticalAlignment="Top" Width="305">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" Header="ID" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding RFID}" Header="RFID" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding name}" Header="Type" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding model}" Header="Model" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding nextMaintenance}" Header="Dato" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding space}" Header="Plads" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>