Editing dIfferent Object Types Using Different Controls in Wpf Datagrid Column

I need to create a Wpf Datagrid binding from ObservableCollection with a dozen vocabularies where string is the property name and object is the property value. Each object can be of a different type, it can be boolean (checkbox), string (text box), CustomClassObject (combobox or text box), integer (text box), or enum (ComboBox with each value from the enumeration).

And it must be filled dynamically.

I've been trying to figure this out since last week, but it's just complicated.

Do you have an idea to create one like datagrid that will solve this problem? How to link such as a list of objects pulled from a dictionary or just an entire dictionary in a datagrid so that the user can easily edit it?

Should I use datatemplate with some converters if possible, which will return the right control with the appropriate value for each object. Or should I create a usercontrol that will consist of a bindable property object that I will assign the correct full control field on each value and it will bind to the ContentControl?

I would be grateful for every hint.

thank

+3


source to share


1 answer


Perhaps something like this:

public class MyObjectList : ObservableCollection<object>
{
    public MyObjectList()
    {
        Add(new KeyValuePair<string, int>("Key1", 1));
        Add(new KeyValuePair<string, string>("Key2", "Value2"));
        Add(new KeyValuePair<string, bool>("Key3", true));
        Add(new KeyValuePair<string, double>("Key4", 1.5));
        Add(new KeyValuePair<string, MyEnum>("Key5", MyEnum.OPTION3));
        Add(new KeyValuePair<string, MyCustomClass>("Key6", new MyCustomClass(123)));
    }
}

public class MyCustomClass
{
    int value;

    public MyCustomClass(int value)
    {
        this.value = value;
    }
    public override string ToString()
    {
        return string.Format("MyCustomClass is {0}", value);
    }
}

enum MyEnum { OPTION1, OPTION2, OPTION3 };

      

XAML:



    <DataGrid Margin="0" ItemsSource="{Binding Mode=OneWay}">
        <DataGrid.DataContext>
            <local:MyObjectList/>
        </DataGrid.DataContext>
    </DataGrid>

      

Result:

enter image description here

+1


source







All Articles