1

DataGrid当 StackPanel 被选中或聚焦时,我必须更改它的背景颜色(存在于 中)。

下面是我的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>

改变Background颜色的最佳方法是什么StackPanel

4

1 回答 1

1

在 DataGridCell 上使用 IsSelected 触发器。如果您不想完全更改所有元素的选定颜色,则可以在应用程序资源中使用以下代码。

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#3271B5" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="AliceBlue" />
于 2013-05-20T08:31:48.787 回答