MvvmLight - Hiding IsInDesignMode in DataGrid



mvvmlight的ViewModelBase类型具有所有子类属性的属性IsInDesignMode

我的MainWindow ViewModel类似于以下内容:

class MainWindowViewModel : ViewModelBase {
    ObservableCollection<PersonViewModel> People { get; }
}
class PersonViewModel : ViewModelBase {
}

我的datagrid的XAML仅仅是这样:

<DataGrid ItemsSource="{Binding Path=People}" />`

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

i也有另一个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属性。

最新更新