2

所有,一个简单的问题。我有一个 MVVM 应用程序DataGrid,我使用它绑定到 ViewModel

<DataGrid ItemsSource="{Binding Path=Resources}" ...></DataGrid>

其中Resources是通过

public ObservableCollection<ResourceViewModel> Resources { get; private set; }

然而,在课堂上,ResourceViewModel不仅有我想出现在. 班级是DataGridDataGridResourceViewmodel

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. 如何防止IsSelected在 中显示 [as a Checkbox] DataGrid

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

你有什么尝试:

  1. 我试图从ResourceViewmodel类继承并绑定到它,但这很恶心,我想要另一个更优雅的解决方案;请 :]。

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

与往常一样,非常感谢您抽出宝贵时间。

4

2 回答 2

5

我认为选项是像 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;
    }
}
于 2013-05-21T21:16:15.017 回答
2

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

  1. 绑定到 IResourceViewModel 的 ObservableCollection - 使 Resources 属性成为接口列表而不是具体类型
  2. 使 ResourceViewModel 实现 IResourceViewModel
  3. 在 IResourceViewModel 从界面中删除您不希望对网格可见的属性
于 2013-05-21T21:29:57.240 回答