Link Combobox to dataGrid wpf column



  • Hello, people! My question is: There's a wpf form. Wpf form

                selectedObjectsDataGrid.ItemsSource = attributesManager.ObjectsInGrid;
    

    Her sorce is an ObjectInGrid leaf.

        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
    }
    

    }

    There are three options in the box (as in enum ObjType).
    The objects come to the degrid in such a way that the user distinguishes one or more objects in the document. The object is created in a rude way.

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

    Property Type, like Type Column, is still empty.
    When the user added all the objects, he singles out one or more objects in the grid, and selects the type in the box.
    введите сюда описание изображения

    SelectionChanged combox finds a id element in a sorcerer leaf and fills a propperty Type.

    I just started working with WPF, but I'm guessing there's a way to tie a combobox column like a normal way, not like I'm handwritten.

    In my approach, I don't like:

    1. The SelectionChanged event works when the websites are changing, but if I choose another object and I need to put the same type as in the past, it's not generated.
    2. I would like to do everything at a higher level.
      Google, I've been looking, but there's nothing specific to this task.



  • Okay, looks like 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"/>

    Like a code-behind isn't necessary at all. But you need to fix the right thing. DataContext


    Yeah, another problem with your code: your ViewModel. ObjectInGriddoesn't operate the interface. INotifyPropertyChanged♪ It's important, you have to implant him. Otherwise it won't work.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2