wpf - MvvmLight-在DataGrid中隐藏IsInDesignMode

标签 wpf mvvm datagrid mvvm-light

MvvmLight的ViewModelBase类型具有由所有子类继承的IsInDesignMode属性。

我的MainWindow ViewModel类似于:

class MainWindowViewModel : ViewModelBase {

    ObservableCollection<PersonViewModel> People { get; }
}

class PersonViewModel : ViewModelBase {
}

我对DataGrid的XAML就是这样:
<DataGrid ItemsSource="{Binding Path=People}" />`

运行应用程序时,我会看到PersonViewModel的所有属性,但IsInDesignMode是其中一列。这是不希望的。

我还具有另一个ViewModel,它表示另一个实体,ProductViewModel通过ObservableCollection<Pair<String,String>>属性具有可扩展的属性,其中每个Pair<String,String>条目分别表示一个附加的列名及其值。

非工作解决方案:

为了解决IsInDesignMode问题,我实现了PersonViewModel : ICustomTypeDescriptor,并在GetProperties方法中删除了IsInDesignMode属性,但是,当DataGrid呈现我的集合时,它仍然具有该列。我在GetProperties中设置了一个断点并被调用,所以我不知道为什么WPF不尊重结果。
class PersonViewModel : ViewModelBase, ICustomTypeDescriptor {
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {
        return new PropertyDescriptorCollection(
            TypeDescriptor.GetProperties( this, attributes, true ).Where( pd => pd.Name != "IsInDesignMode" )
        );
    }
}

我也将ObservableCollection<PersonViewModel> People更改为TypedListObservableCollection<PersonViewModel>,这是具有以下定义的类:
public class TypedListObservableCollection<T> : ObservableCollection<T>, ITypedList
{
    public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
    {
       return TypeDescriptor.GetProperties( typeof(T));
    }

...但是,这不会导致WPF尊重我的逻辑并隐藏IsInDesignMode列。

最佳答案

将ViewModelBase继承替换为ObservableObject。
这是一个较轻的基类,但包含所有INotifyPropertyChange封装,但没有IsInDesign属性。

关于wpf - MvvmLight-在DataGrid中隐藏IsInDesignMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30093895/

相关文章:

c# - 更改 DataGrid 列标题文本

c# - 在 WPF Windows 的不同状态下以矩形显示或隐藏问题

c# - 将 GridView 绑定(bind)到列表

wpf - 使用 MVVM ViewModel 将 XDocument 显示为 WPF TreeView

c# - 数字增量太多

c# - RxUI - WPF - 不带 DataContext 的样式 DataTrigger 绑定(bind)

c# - 当模型中的列表更改时,ViewModel 中的 ObservableCollection 不会更新

c# - mvvm一般的疑惑和messageBox

.net - wpf datagrid rowdetail 被截断

c# - 根据不同列的值更改单元格背景