Bind customObject to DataGrid

The basics of binding a list of objects to a DataGrid are discussed here How to bind a <CustomObject> list in a WPF DataGrid? ...

My setup is a little more complicated: I have RowModels

one that contains metadata for a string as well as a list CellModels

. CellModels

again contains some metadata and a property Value

that is shown in the dataGrid.

To populate my DataGrid I have set

ItemsSource="{Binding RowModelList, UpdateSourceTrigger= PropertyChanged}"

      

and mapped the columns one by one through

<DataGridTextColumn Binding="{Binding CellModelList[0].Value, UpdateSourceTrigger= PropertyChanged}"/>
<DataGridTextColumn Binding="{Binding CellModelList[1].Value, UpdateSourceTrigger= PropertyChanged}"/>
...

      

This way I could also specify a ColumnHeader for each column and change the view of each column.

However, I would like to use -Style DataTrigger

in my DataGridCell

to access the metadata of my CellModels.

this works, but .. well :

Of course I could create a style for each column like

<Style x:Key="CellStyleColumnZero" TargetType="DataGridCell">
    <Style.Triggers>
        <DataTrigger Binding="{Binding CellModelList[0].Enabled}" Value="False">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

      

Question:

I need to set properties DataGridCell

depending on the metadata in mine CellModel

.

I am sure there is a more elegant solution (than shown above) that does not require setting up every single column. I still need to be able to manually modify individual columns (especially ColumnHeaders

), so I manually declared the DataGridTextColumns one by one.

Do you have any suggestions on how to approach it correctly?

+2


source to share


1 answer


This was answered by the following question.

The solution I used was to create an attached property for DataGridCell

where I store the CellModel and access the properties from there.



See the linked question for code: WPF DataGrid Structure - Changing Cells Based on Value

0


source







All Articles