繁体   English   中英

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

[英]Limit the Properties Bound to DataGrid in WPF using MVVM

一切都是一个简单的问题。 我有一个带有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

一如既往,非常感谢您的宝贵时间。

我认为选项是关闭Silvermind提到的自动生成(即将DataGrid.AutoGenerateColumns设置为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-使Resources属性成为接口列表,而不是具体类型
  2. 使ResourceViewModel实现IResourceViewModel
  3. 在IResourceViewModel中,从界面中删除网格中不希望看到的属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM