DataGrid上的WPF样式触发器被绑定覆盖

时间:2014-06-23 19:32:53

标签: wpf .net-4.0 wpfdatagrid wpf-style

我找到了一种方法,使用:https://stackoverflow.com/a/17089837/1646494在wpf数据网格中将布尔值转换为是/否而不是True / False,但是一旦完成,此列就不可排序。我的代码如下:

<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>

尝试让排序再次起作用只会使列恢复为显示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>

是否有解决方案,不涉及转换器?

1 个答案:

答案 0 :(得分:1)

如果您想根据给定的DataGrid列进行更改,可以使用DataGridColumn.SortMemberPath

  

获取或设置属性名称或属性名称的句点分隔层次结构,指示要按其排序的成员

所以在你的XAML中你可以做到

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