【问题标题】:WPF Datagrid Selected Row Color and Centered TextWPF Datagrid 选定行颜色和居中文本
【发布时间】:2015-01-23 06:43:18
【问题描述】:

我有一个简单的DataGrid,我想在其中设置所选行的样式并将文本居中。我尝试了以下方法,但它不起作用:

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Blue" />
                <Setter Property="Foreground" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.CellStyle>

为什么以上不能一起工作?如果我移除触发器,它将居中,但不会使用我想要的颜色。

【问题讨论】:

    标签: wpf xaml datagrid styles


    【解决方案1】:

    尝试将Grid添加到DataGridCell样式的ControlTemplate中:

    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="Transparent" />
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter Margin="2"
                                          VerticalAlignment="Center"
                                          HorizontalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White" />
                <Setter Property="Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多