Datagrid binding several properties of one object to column

1.7k views Asked by At

I have an object with several properties. However some of these properties contain information of the same group so to say. I would like to show these properties in a table in a way that in the same column i could put the properties of the same group. However I am not sure how to do that exactly. I do understand how to do it if i had several objects of the same type (put them in a list and get the default collection view source). But in this case i just have one object with several properties.

My question is: how could I bring these properties together to make one column in datagrid. all the properties are of same datatype (ushort).

Just to illustrate what i mean:


public ushort MyProperty1
{
    get{ return myProperty1}
    set
    {
       if(value==myProperty1)
         return;
       myProperty1=value;
       OnPropertyChanged("MyProperty1");    
    }
}

public ushort MyProperty2
{
    get{ return myProperty2}
    set
    {
       if(value==myProperty2)
         return;
       myProperty2=value;
       OnPropertyChanged("MyProperty2");    
    }
}

public ushort MyProperty3
{
    get{ return myProperty3}
    set
    {
       if(value==myProperty3)
         return;
       myProperty3=value;
       OnPropertyChanged("MyProperty3");    
    }
}

And i would like to show these properties in the same column of datagrid

Thanks :)

2

There are 2 answers

1
Sheridan On BEST ANSWER

You've got a few options to acieve your goal. You could use a MultiBinding with a StringFormat in a standard DataGridTextColumn.

Alternatively, you could define a DataTemplate and set it as the CellTemplate property of a DataGridTemplateColumn element.

I've included links to help get you started.

1
Kumareshan On

you can try the below one

    <DataGrid ItemsSource="{Binding Values}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Values">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding MyProperty1}"/>
                            <TextBox Text="{Binding MyProperty2}"/>
                            <TextBox Text="{Binding MyProperty3}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>