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