使用MVVM限制WPF中绑定到DataGrid的属性

本文关键字:DataGrid 属性 绑定 MVVM 限制 WPF 使用 | 更新日期: 2023-09-27 18:14:56

总之,一个简单的问题。我有一个带有DataGrid的MVVM应用程序,我已使用

绑定到ViewModel
<DataGrid ItemsSource="{Binding Path=Resources}" ...></DataGrid>

其中Resources通过

定义
public ObservableCollection<ResourceViewModel> Resources { get; private set; }
然而,在ResourceViewModel类中,我不仅具有我希望出现在DataGrid中的属性,而且具有我希望出现在DataGrid中的其他属性。ResourceViewmodel类是
public class ResourceViewModel : WorkspaceViewModel, IDataErrorInfo
{
    readonly Resource resource;
    readonly ResourceDataRepository resourceRepository;
    private bool isSelected;
    public ResourceViewModel(Resource resource, ResourceDataRepository resourceRepository)
    {
        if (resource == null)
            throw new ArgumentNullException("resource");
        if (resourceRepository == null)
            throw new ArgumentNullException("resourceRepository");
        this.resource = resource;
        this.resourceRepository = resourceRepository;
    }
    public string KeyIndex 
    { 
        get { return this.resource.KeyIndex; } 
        set 
        {
            if (value == this.resource.KeyIndex)
                return;
            this.resource.KeyIndex = value;
            base.OnPropertyChanged("KeyIndex");
        }
    }
    public string FileName
    {
        get { return this.resource.FileName; }
        set 
        {
            if (value == this.resource.FileName)
                return;
            this.resource.FileName = value;
            base.OnPropertyChanged("FileName");
        }
    }
    public List<string> ResourceStringList
    {
        get { return this.resource.ResourceStringList; }
        set 
        {
            if (Utilities.Utilities.ScrambledEquals<string>(this.resource.ResourceStringList, value))
                return;
            this.resource.ResourceStringList = value;
            base.OnPropertyChanged("ResourceStringList");
        }
    }
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            if (value == isSelected)
                return;
            isSelected = value;
            base.OnPropertyChanged("IsSelected");
        }
    }
}

我不希望IsSelected出现在DataGrid中,我希望ResourceStringList中的每个项目出现在Datagrid的不同列中。我的问题是:

1。如何防止IsSelectedDataGrid中显示[作为Checkbox] ?

2。如何将绑定到DataGrid以自动在单独的列中显示项目?

你试过什么?

  1. 我已经尝试从ResourceViewmodel类继承并绑定到这个,但这是令人厌恶的,我想要另一个更优雅的解决方案;请:].

  2. 我不知道如何进行这个。List中存储的项目数量是可变的,并在运行时设置-所以这需要是List

与往常一样,非常感谢您的时间。

使用MVVM限制WPF中绑定到DataGrid的属性

我认为选项是关闭自动生成,正如Silvermind提到的(即设置DataGrid)。autogeneratecolcolumns为false,然后定义列)或实现ITypedList。例如,您可以创建派生的ObservableCollection,它实现了ITypedList,并根据您想要隐藏的属性上的某些属性返回属性。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.DataContext = new TypedListObservableCollection<Foo>();
        InitializeComponent();
    }
}
public class TypedListObservableCollection<T> : ObservableCollection<T>
    , ITypedList
{
    public TypedListObservableCollection()
    {
    }
    PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
    {
        return TypeDescriptor.GetProperties(typeof(T), new Attribute[] { BrowsableAttribute.Yes });
    }
    string ITypedList.GetListName(PropertyDescriptor[] listAccessors)
    {
        return typeof(T).Name;
    }
}
public class Foo
{
    public string Name
    {
        get;
        set;
    }
    [Browsable(false)]
    public bool IsSelected
    {
        get;
        set;
    }
}

对我来说,不自动生成列更容易。但这是个人偏好,所以我认为不允许某些属性暴露的最简单方法是使用接口的功能:)

  1. 绑定到IResourceViewModel的ObservableCollection -使资源属性成为接口列表而不是具体类型
  2. 使ResourceViewModel实现IResourceViewModel
  3. 在IResourceViewModel
  4. 中删除你不想在网格中显示的属性