wpf - DataGrid - 将自定义对象与子列表绑定(bind)

标签 wpf data-binding binding mvvm datagrid

我有一个

ObservableCollection<CustomObj> DataInfo 

在我的 MVVM WPF 项目中。
CustomObj 类如下所示:
public class class1 : ObservableObject
{
    public class1()
    {
        MySecondProperty = new Class2();
    }

    public string MyStringValue { get; set; }
    public Class2 MySecondProperty { get; set; }
    public List<Class3> MyThirdProperty{ get; set; }
}

每当我像这样绑定(bind) WPF 属性时
<DataGrid Name="dgMyDataGrid" 
              SelectedItem="{Binding SelectedItem}"
              ItemsSource="{Binding DataInfo}">
</DataGrid>

我从“MyStringValue”以及我的数据网格中的对象和集合中获取值。
谷歌没有给我任何结果,我找不到与此示例类似的任何内容。

如何以一种简单的方式从 Class2 和 List 中获取数据以显示数据?

最佳答案

您需要定义列并在列定义内绑定(bind)。

下面将在第二列显示 MySecondProperty.SubProperty 的值

对于 Class3,如果您想要组合框之类的东西,请使用模板化的数据网格列
http://blogs.msdn.com/b/vinsibal/archive/2008/08/19/wpf-datagrid-stock-and-template-columns.aspx
有关于列模板的信息

<DataGrid Name="dgMyDataGrid" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding DataInfo}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="MyStringValue " Width="*" Binding="{Binding Path=MyStringValue }" />
    <DataGridTextColumn Header="MySecondProperty.SubProperty" Width="*" Binding="{Binding Path=MySecondProperty.SubProperty}" />
  </DataGrid.Columns>
</DataGrid>

关于wpf - DataGrid - 将自定义对象与子列表绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8476789/

相关文章:

binding - let-rebinding 和标准分配有什么区别?

WPF CheckBox 的 IsChecked 属性与绑定(bind)源的值不匹配

wpf - 如何将 WPF Datagrid RowHeaderWidth 设置为自动?

c# - WPF 图像控件源绑定(bind)

c# - 如何在 WPF 和 ASP.NET 应用程序之间进行通信?

wpf - 使用拆分器后如何重置网格行高?

.net - DataGridView 排序和例如.NET 中的 BindingList<T>

绑定(bind)到 ObservableCollection<string> 的 Silverlight TabControl 在集合更改时不更新

c# - 更改绑定(bind)属性时,ListView 分组不会更新

asp.net-mvc - 参数名称必须是model吗?