Search code examples
wpf.net-4.0wpfdatagridwpf-style

WPF Style Triggers on DataGrid being overwritten by binding


I found a way to convert a boolean to a yes/no instead of True/False in a wpf datagrid using: https://stackoverflow.com/a/17089837/1646494, however this column is not sortable once this is done. My code is below:

<DataGridTextColumn Header=" Consolidated Company " IsReadOnly="True" MinWidth="100">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="Margin" Value="5,0,5,0" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding lRollupCompany}" Value="True">
                    <Setter Property="Text" Value="Yes" />
                </DataTrigger>
                <DataTrigger Binding="{Binding lRollupCompany}" Value="False">
                    <Setter Property="Text" Value="No" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

Trying to get the sorting to work again just makes the column revert back to showing True/False...

        <DataGridTextColumn Header=" Consolidated Company " Binding="{Binding lRollupCompany}" IsReadOnly="True" MinWidth="100">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Left" />
                    <Setter Property="Margin" Value="5,0,5,0" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding lRollupCompany}" Value="True">
                            <Setter Property="Text" Value="Yes" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding lRollupCompany}" Value="False">
                            <Setter Property="Text" Value="No" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>

Is there a solution to this that doesn't involve a converter?


Solution

  • If you want to change by what given DataGrid column is sorted you can use DataGridColumn.SortMemberPath

    Gets or sets a property name, or a period-delimited hierarchy of property names, that indicates the member to sort by

    so in your XAML you can do

    <DataGridTextColumn  ... SortMemberPath="lRollupCompany"/>