如何保持Enabled = False的功能,同时保持Enabled = True的样式?

时间:2014-08-07 13:23:50

标签: c# .net wpf xaml datagrid

我的DataGrid看起来像这样:

这是我目前的代码:

<DataGrid x:Name="DataGrid" HorizontalAlignment="Center" VerticalAlignment="Center"
          ColumnWidth="100" ColumnHeaderHeight="50" RowHeight="50" RowHeaderWidth="115" Padding="5"
          BorderBrush="#FF646464" FontSize="18" FontFamily="Segoe UI"
          CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False"
          Focusable="False" IsEnabled="False" IsReadOnly="True">
    <DataGrid.Background>
        <SolidColorBrush Color="#FFFFFFC8"/>
    </DataGrid.Background>
    <DataGrid.Columns>
        <DataGridTextColumn CellStyle="{StaticResource DataGridCellStyle}" Binding="{Binding In}" Header="In"/>
        <DataGridTextColumn CellStyle="{StaticResource DataGridCellStyle}" Binding="{Binding Out}" Header="Out"/>
        <DataGridTextColumn CellStyle="{StaticResource DataGridCellStyle}" Binding="{Binding Hours}" Header="Hours"/>
    </DataGrid.Columns>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Background" Value="#FFFFFFC8"/>
            <Setter Property="BorderBrush" Value="DarkSlateGray"/>
            <Setter Property="BorderThickness" Value="1, 2"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="5"/>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <DataGrid.RowBackground>
        <SolidColorBrush Color="Transparent"/>
    </DataGrid.RowBackground>
    <DataGrid.RowHeaderStyle>
        <Style TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Background" Value="#FFFFFFC8"/>
            <Setter Property="BorderBrush" Value="DarkSlateGray"/>
            <Setter Property="BorderThickness" Value="2, 1"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Padding" Value="5"/>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Item.Day}"/>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>

我希望在功能方面保持DataGrid禁用,但我希望将文字保持为黑色(而不是灰色)。我怎样才能做到这一点?

我的DataGridCellStyle位于<Window.Resources>

之内
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

2 个答案:

答案 0 :(得分:3)

无需弄乱样式,只需将DataGrid的IsHitTestVisible设置为False

您也可以删除CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" Focusable="False"

<DataGrid x:Name="DataGrid" HorizontalAlignment="Center" VerticalAlignment="Center"
          ColumnWidth="100" ColumnHeaderHeight="50" RowHeight="50" RowHeaderWidth="115" Padding="5"
          BorderBrush="#FF646464" FontSize="18" FontFamily="Segoe UI"
          IsHitTestVisible="False">
....

答案 1 :(得分:0)

您可以尝试覆盖数据网格中的默认禁用画笔,方法是将颜色与控件在资源中使用IsEnable = False时使用的键相同。

  <DataGrid.Resources>
    <SolidColorBrush 
       x:Key="{x:Static SystemColors.GrayTextBrushKey}" 
       Color="{x:Static SystemColors.ControlTextColor}" />
  </DataGrid.Resources>

P.S。我没有检查datagrid的默认单元格模板,我只是假设它使用了GrayTextBrushKey,所以先检查一下。