【问题标题】:How to populate a class to a grid如何将类填充到网格中
【发布时间】:2018-04-27 17:41:04
【问题描述】:

我有一个类InfoField 包含各种类型的成员。我想将ObservableCollection<InfoField> 属性绑定到某种 WPF 控制器。

  • 类成员水平列出,所有集合元素垂直列出。
  • 列和行都有标题。
  • 我可以为各种数据类型应用不同的数据模板。
  • 我可以按列标题(例如 ShowRpm)来选择它下面的所有复选框。

我一直在使用 ListView 进行测试,但我不确定这是正确的方法。据我所知,我很难使用ListView 获得第二点和第四点。任何人都知道为此使用哪个控件?

          ShowRpm    ShowCurrent  ShowTemperature  Direction
Motor1   (checkbox)  (checkbox)   (checkbox)       (combobox)
Motor2   (checkbox)  (checkbox)   (checkbox)       (combobox)
Motor3   (checkbox)  (checkbox)   (checkbox)       (combobox)
 ...
MotorN   (checkbox)  (checkbox)   (checkbox)       (combobox)

C#:

public class InfoField {
    public bool ShowRpm { get; set; }
    public bool ShowCurrent { get; set; }
    public bool ShowTemperature { get; set; }
    public Direction Direction { get; set; }
}

public enum Direction {
    Horizontal,
    Vertical
}

【问题讨论】:

  • 我会考虑使用DataGridView。您可能需要进行一些定制才能满足您的所有要求,但这应该是一个好的开始。

标签: c# wpf


【解决方案1】:

我建议您使用 DataGrid。通过一些自定义,您应该能够统计所有选项。这是您可以使用的起点

//Collection for the datagrid
public ObservableCollection<InfoField> infoFieldData;

public MainWindow()
{
    InitializeComponent();

    infoFieldData = new ObservableCollection<InfoField>();

    InfoField info1 = new InfoField();
    InfoField info2 = new InfoField();

    infoFieldData.Add(info1);
    infoFieldData.Add(info2);

    dataGrid.ItemsSource = infoFieldData;
    dataGrid.Items.Refresh();
}



public class InfoField
{
    public bool ShowRpm { get; set; }
    public bool ShowCurrent { get; set; }
    public bool ShowTemperature { get; set; }
    public Direction Direction { get; set; }
}

public enum Direction
{
    Horizontal,
    Vertical
}

DataGrid XAML

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" VerticalAlignment="Top" CanUserAddRows="False"/>

对于第 2 点,有行标题有这个问题DataGrid Row Header WPF

第 4 点是How do I capture “Click” events on a DataGrid column headers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2021-05-31
    相关资源
    最近更新 更多