WPF converter column ItemsSource

481 views Asked by At

I want to add a converter in my DataGrid that is binded with an itemsSource.

        <DataGrid x:Name="DataGrid1" ItemsSource="{Binding List}"/>

And ItemSource has been binded with a list. There is a boolean property (IsClientGood). I want that my datagrid doesn't show a checkbox but a color thanks to a converter. Here is the converter. Thank you guys for your help !

1

There are 1 answers

3
Dennis On

I assume, that you don't know, where to place the converter? If you want to customize the look of DataGrid, you must avoid generating columns from properties, ad define them manually.

Something like this:

<DataGrid x:Name="DataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding ClientList}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Is client good">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Rectangle Fill="{Binding IsClientGood, Converter={StaticResource BoolToColorConverterKey}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <!-- Other columns here -->
    </DataGrid.Columns>
<DataGrid>

where BoolToColorConverterKey is a key of resource, that defines your BoolToColor converter.