[英]How is it possible to change datagrid cell background color when is selected and focused?
[英]How to change background color of stack panel when selected in datagrid
当它被选中或聚焦时,我必须更改StackPanel(存在于DataGrid
)的背景颜色。
以下是我的XAML
:
<DataGrid Background="#DCE1E7" SelectedItem="{Binding Row, Mode=TwoWay}" AutoGenerateColumns="False" CanUserAddRows="False" HeadersVisibility="None" GridLinesVisibility="None" HorizontalAlignment="Left" BorderThickness="0" Name="dtlCuisine" VerticalAlignment="Top" BorderBrush="#DCE1E7" VerticalGridLinesBrush="#DCE1E7" HorizontalGridLinesBrush="#DCE1E7" ColumnWidth="130" SelectionChanged="dtlCuisine_SelectionChanged" CellEditEnding="dtlCuisine_CellEditEnding">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="Template13">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Name="stkpnlCuisines" Orientation="Horizontal" Background="#DCE1E7" Margin="-1,-1,-1,-1" MouseDown="stkpnlCuisines_MouseDown_1">
<Label Content="{Binding CuisineName}" Height="28" HorizontalAlignment="Left" Name="lblCuisineName" VerticalAlignment="Top" FontSize="14" />
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsFocused}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Margin="-1,-1,-1,-1">
<TextBox Name="txtCuisineCategory" AcceptsReturn="True" Text="{Binding CuisineName}" LostFocus="txtCuisineCategory_LostFocus" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
更改StackPanel
的Background
颜色的最佳方法是什么。
在DataGridCell上使用IsSelected触发器。 如果您不想完全更改所有元素的所选颜色,则可以在应用程序资源中使用以下代码。
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#3271B5" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="AliceBlue" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.