簡體   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