简体   繁体   English

禁用WPF数据网格中的行

[英]Disable rows in WPF datagrid

I have an observablecollection bound to a datagrid. 我有一个绑定到datagrid的observablecollection。 This collection is populated from a search result. 该集合是从搜索结果中填充的。 I can also add items to the datagrid. 我还可以将项目添加到数据网格。 I want to disable rows, that were part of the original collection. 我想禁用行,这些行是原始集合的一部分。 The new rows added by me, should be editable. 我添加的新行应可编辑。 My limitation is that, I cannot have a boolean property in the collection to use in a datatrigger. 我的局限性在于,我不能在数据触发器中使用集合中的布尔属性。 Is there a way to do it? 有办法吗?

Edit: Forgot to mention that I am using a MVVM framework. 编辑:忘记提及我正在使用MVVM框架。

If you cannot add "IsEnabled" property to items in your collection you can always create new class which inherits from class/type of those items. 如果您不能向集合中的项目添加“ IsEnabled”属性,则始终可以创建从这些项目的类/类型继承的新类。

    public class MyDataGridPresenterClass : OriginalItemClass
    {
        public bool IsEnabled { get; set; }
    }

And use this new type in that ObservableCollection Nothing can stop you from doing this... And of course every MyDataGridPresenterClass is also OriginalItemClass since it inherits from it. 并在ObservableCollection使用这种新type ,没有什么可以阻止您这样做的。当然,每个MyDataGridPresenterClass也是OriginalItemClass因为它是从它继承的。

So you can have 所以你可以有

    ObservableCollection<MyDataGridPresenterClass> DataGridItems
    {
        get { return this.dataGridItems; }
        set
        {
            this.dataGridItems = value;
            RaisePropertyChanged("DataGridItems");
        }
    }

Now You have IsEnabled property so you can use it in that DataTrigger you mentioned, you didn't modify OriginalItemClass and everybody is happy. 现在,您具有IsEnabled属性,因此可以在您提到的DataTrigger中使用它,而无需修改OriginalItemClass ,每个人都很高兴。 :) And one more thing. :) 还有一件事。 Just to be clear :) MVVM is not a framework... MVVM is design pattern. 只是要清楚:) MVVM不是框架... MVVM是设计模式。 I hope this helps :) Best regards and good luck :) 我希望这会有所帮助:)最好的问候和好运:)

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

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