Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

c# - How to change specific property in an element of an Observable Collection on PropertyChange?

I have a DataGrid with more columns and rows. What I'd like is to make that if I change a ComboBox in row 1, the Combo in row 2 should chage with it, but nothing else. I use an Observable Collection which stores the classes. I have no idea how to make it...

The Collection:

private ObservableCollection<Item> ceilingItems = new ObservableCollection<Item>();
ceilingItems.Add(new Item() { Category = language.GetString("top-mesh"), Name = "4. " + language.GetString("main-direction"), Value1 = list1, Unit1 = "mm", Value2 = list2, Unit2 = "cm", IsEditable = true });
ceilingItems.Add(new Item() { Category = "", Name = "3. " + language.GetString("side-direction"), Value1 = list1, Unit1 = "mm", Value2 = list2, Unit2 = "cm", IsEditable = false });
ceilingItems.Add(new Item() { Category = language.GetString("bottom-mesh"), Name = "2. " + language.GetString("side-direction"), Value1 = list1, Unit1="mm", Value2 = list2, Unit2 = "cm", IsEditable = false });
ceilingItems.Add(new Item() { Category = "", Name = "1. " + language.GetString("main-direction"), Value1 = list1, Unit1 = "mm", Value2 = list2, Unit2 = "cm", IsEditable = true });
ceilingItems.Add(new Item() { Category = language.GetString("lace"), Name = "", Value1 = list1, Unit1 = "mm", Value2 = list2, Unit2 = "cm", IsEditable = true });
ceilingItems.Add(new Item() { Category = language.GetString("spacer-iron"), Name = "", Value1 = list1, Unit1="mm", Value2 = list3, Unit2 = language.GetString("piece") + " / m2", IsEditable = true });

For example: if I change Value1 in the first element, Value1 has to be the same in the second element, but just there. (This is because of the function for what we will use them...) This collection is binded to DataGrid Columns.

The Item class:

public class Item: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Category { get; set; }
    public string Name { get; set; }
    public List<int> Value1 { get; set; }
    public string Unit1 { get; set; }
    public List<double> Value2 { get; set; }
    public string Unit2 { get; set; }
    public bool IsEditable { get; set; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to play with SelectedValue of your Comboboxes in row 1 and 2.

Supposing both Comboboxes are Binded to the same collection so you can guarantee that the possible values of both are always the same, then you have to sync ComboBoxes SelectedValue property: once the SelectedValue changes on ComboBox at row 1 you need to set the SelectedValue of ComboBox at row 2 to the same value.

Some hints to do it.

Way 1

Let the Grid ViewModel synch the SelectedValue supposing it contains an ObservableCollection<ViewModelRow>. This way is some boring cause you need to implement a mechanism to monitor the synch the SelectedValue changes of any synch the ViewModelRow in your collection.

Way 2

This solution get you better code maintenance and also let you code less than the first solution.

You need to use Message Broker design pattern https://en.wikipedia.org/wiki/Message_broker you can implement your own Message Broker or simply use one of any MVVM Toolkit libraries that already implement it. Most of the existing MVVM Toolkits have their own Message Broker implementation. So no need to re-invent the whell.

For example, the MVVM Light Toolkit Message Broker is explained here: https://docs.microsoft.com/en-us/archive/msdn-magazine/2014/june/mvvm-the-mvvm-light-messenger-in-depth#using-messages

So you can let the broker send a Message when SelectedValue of ComboBox at first row changes then the ViewModel Binded to ComboBox at second row can subscribe to this message and react selecting its own SelectedValue property accordingly.

Warning

Does the behavior you described be the same in case the SelectedValue of Combobox at row 2 changes? If yes then be carefull to avoiding loops that must be handled.

I hope this help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...