【问题标题】:Only bind certain properties of BindingList to DataGrid仅将 BindingList 的某些属性绑定到 DataGrid
【发布时间】:2012-10-28 22:03:32
【问题描述】:

一些背景:

public class Base
{
    public string A { get; set; }
    public double B { get; set; }
    public DateTime C { get; set; }
    public string D { get; set; }
}

public class DerivedClass : Base
{
    public string E { get; set; }
    public List<string> F { get; set; }
    public List<string> G { get; set; }
    public Image H { get; set; }
}

Base 类用作其他几个具有共同属性的类的基础。派生类添加特定于该派生类的属性。

在我的视图中,我有一个绑定到我的 DerivedClassViewModelBindingList&lt;DerivedClass&gt; 的 DataGrid:

<DataGrid Margin="318,0,0,0" ItemsSource="{Binding BindingList}">

这很好用,我可以在 BindingList 中添加和删除项目,DataGrid 会自动更新以反映这一点。但是,DerivedClass 有几个我不想在 DataGrid 中显示的属性(例如 List&lt;string&gt; F/H)。 如何在仍使用BindingList&lt;DerivedClass&gt; 的同时仅将 DerivedClass 的某些属性绑定到 DataGrid?

【问题讨论】:

    标签: c# wpf mvvm wpfdatagrid


    【解决方案1】:

    您必须在 DataGrid 上禁用 AutoGenerateColumns 并明确定义列 http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.columns(v=vs.100).aspx,或者使用 AutoGeneratingColumn 事件丢弃您不想要的列 http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.autogeneratingcolumn(v=vs.100).aspx

    【讨论】:

    • 我希望避免这种情况,但我想它必须这样做。
    【解决方案2】:

    @see BindingList(IList)(“构造函数”下的第二项)

    【讨论】:

    • 我不确定这对我有什么帮助,因为我必须指定一个只包含我想要的 DerivedClass 属性的新类型。
    猜你喜欢
    • 2013-07-07
    • 2012-12-28
    • 1970-01-01
    • 2011-08-29
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多