Greetings, people! My question is this: There is a wpf form Wpf form

selectedObjectsDataGrid.ItemsSource = attributesManager.ObjectsInGrid; 

Its source is a list of ObjectInGrid objects.

  public class ObjectInGrid { private ObjType? type; public Guid Object { get; set; } public ObjType? Type { get { return type; } set { type = value; } } public ObjectInGrid(Guid obj) { Object = obj; } public enum ObjType { None = 0, Door = 1, Window = 2 } } 

Combobox has three options (as in enum ObjType). Objects are sent to the datagrid as follows: the user selects one or more objects in the document by pressing, roughly speaking, an object is created

  public void AddObjectToGrid(Guid id) { ObjectInGrid o = new ObjectInGrid(id); ObjectsInGrid.Add(o); } 

Property Type, as well as Type column while empty. when the user has added all the objects, he selects one or several objects in the grid and selects the type in the combo box. enter image description here

At the SelectionChanged event, the combo box finds by id the element in the list-source and fills the Type with the property.

I just started working with WPF, but I suppose that there is an opportunity to somehow “tie up” a type column and combo box in a normal way, and not like mine.

I don't like my approach:

  1. The SelectionChanged event is triggered when the selected item changes, but if I select another object and I need to set the same type for it as in the past, it will not be generated.
  2. I would like to do everything at a higher level. She searched for Google, but did not find anything concrete for this task.
  • Now I will try to understand exactly what you want. :) In any case, you need to enter MVVM (read!). - VladD
  • Question: do you have to select objects one by one, or several at once in Select Objects ? In case of multiple, what should be shown in Select Type ? - VladD
  • So, I answer. I can allocate a lot of objects. Select and select the type in the combo box. in this case, the selected type is assigned to each object. - Zombik
  • VladD, I read, be sure to - Zombik
  • And for the case of selected several objects, which Type shown in the combo box? - VladD

1 answer 1

Okay, apparently, you need this:

 <ItemsControl ItemsSource="{Binding ObjectsInGrid}" SelectedValue="{Binding SelectedObjectInGrid}" ...> <!-- ... --> </ItemsControl> 
 <Window.Resources> <ObjectDataProvider x:Key="ObjTypeEnumValues" MethodName="GetValues" ObjectType="{x:Type sys:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type Type="src:ObjType" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <Combobox ItemsSource="{Binding Source={StaticResource ObjTypeEnumValues}}" SelectedValue="{Binding SelectedObjectInGrid}" DisplayMemberPath="Type"/> 

It seems that the code-behind is not needed at all. But you need to set the correct DataContext .


Yes, another problem of your code: your ViewModel (that is, the class to which Binding will be), namely, the ObjectInGrid class, does not implement the INotifyPropertyChanged interface. This is important, be sure to implement it. Otherwise it will not work.

  • Wow, thanks! it really is, as it seems to me, what is needed !! - Zombik
  • @Zombik: Very good. If it does not work out, come and ask. - VladD
  • @Zombik: I wrote about INotifyPropertyChanged . It is important. - VladD