【问题标题】:How to keep the functionality of Enabled = False, whilst keeping the styles of Enabled = True?如何保持 Enabled = False 的功能,同时保持 Enabled = True 的样式?
【发布时间】:2014-08-07 13:23:50
【问题描述】:

我有一个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&lt;Window.Resources&gt; 内:

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

【问题讨论】:

  • 请发布您的DataGridCellStyle
  • @FlatEric 我已经更新了我的问题 - 谢谢!
  • IsReadOnly 不是您正在寻找的吗? - NVM 我看到你已经在使用它了。
  • 如果您只是想阻止编辑 - 那么只设置 IsReadOnly,并将 IsEnabled 保留为 true。
  • @Mike 这具有当您单击一行时,该行的文本变为白色的效果。此外,左上角的全选按钮保持启用状态。

标签: c# .net wpf xaml datagrid


【解决方案1】:

不需要弄乱样式,只需将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">
....

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多