我有一个带有DataGrid的WPF表单。当表单加载或没有焦点时,第一个单元格上会出现白色突出显示。如果我单击gridview或当表单获得焦点时,当前单元格将变为蓝色(我设置的选择颜色)。见图:
失去焦点聚焦了
我希望在数据网格没有焦点时删除白色突出显示,即它应该看起来像该行中的其他单元格。我必须处理的财产或事件是什么?
<DockPanel Name="DocHolder" >
<DataGrid x:Name="dataGrid1" MaxHeight="100" AutoGenerateColumns="False" HeadersVisibility="Column" SelectionUnit="Cell" FontSize="11"
VerticalContentAlignment="Center"
HorizontalGridLinesBrush="#FFC7BDBD" AlternatingRowBackground="#690BB9F8"
EnableColumnVirtualization="False" EnableRowVirtualization="False" GridLinesVisibility="None"
CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
ClipboardCopyMode="ExcludeHeader" HorizontalScrollBarVisibility="Disabled" MinRowHeight="20" OverridesDefaultStyle="False"
Style="{DynamicResource scGrid}" ColumnHeaderStyle="{DynamicResource scGridHeader}">
<DataGrid.BorderBrush> <SolidColorBrush /> </DataGrid.BorderBrush>
<DataGrid.RowBackground> <SolidColorBrush /></DataGrid.RowBackground>
<DataGrid.Background> <SolidColorBrush /> </DataGrid.Background>
<DataGrid.Columns>
<DataGridTextColumn Header="GID" DisplayIndex="0" Visibility="Hidden" Binding="{Binding GID}" />
<DataGridTextColumn Header="Region" DisplayIndex="1" IsReadOnly="True" Binding="{Binding AreaName}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridTextColumn Header="Grps" DisplayIndex="2" IsReadOnly="True" Binding="{Binding GName}" CellStyle="{StaticResource FocusAndShowElementData}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridTextColumn Header="Thk" DisplayIndex="3" Binding="{Binding Thick}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="TX" DisplayIndex="4" Binding="{Binding TopX}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="TY" DisplayIndex="5" Binding="{Binding TopY}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="BX" DisplayIndex="6" Binding="{Binding BottomX}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="BY" DisplayIndex="7" Binding="{Binding BottomY}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridComboBoxColumn Header="DTX" DisplayIndex="8" TextBinding ="{Binding BTopX, Mode=TwoWay}" x:Name="cmbBTopX" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="STX" DisplayIndex="9" Binding="{Binding BTopX, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridComboBoxColumn Header="DTY" DisplayIndex="10" TextBinding ="{Binding BTopY, Mode=TwoWay}" x:Name="cmbBTopY" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="STY" DisplayIndex="11" Binding="{Binding BTopY, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridComboBoxColumn Header="DBX" DisplayIndex="12" TextBinding ="{Binding BBottomX, Mode=TwoWay}" x:Name="cmbBBottomX" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="SBX" DisplayIndex="13" Binding="{Binding SBottomX, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridComboBoxColumn Header="DBY" DisplayIndex="14" TextBinding ="{Binding BBottomY, Mode=TwoWay}" x:Name="cmbBBottomY" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="SBY" DisplayIndex="15" Binding="{Binding BBottomY, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
</DataGrid.Columns>
</DataGrid>
</DockPanel>
答案 0 :(得分:1)
您必须覆盖datagrid单元格的单元格模板
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这只是一个你可以参考的例子,像这样你可以做任何你想要的模板
希望这有帮助。