C# 如何更改wpf DataGrid中选定的行背景图像

C# 如何更改wpf DataGrid中选定的行背景图像,c#,wpf,wpf-controls,wpfdatagrid,C#,Wpf,Wpf Controls,Wpfdatagrid,我有以下代码的DataGrid 在alternative行背景中有我自己的图像。所以可能的话,我还想改变选定行上的行背景图像 <DataGrid x:Name="dtstandardview" BorderThickness="0" Height="429" Width="688" BorderBrush="Aqua" MouseLeftButtonDown="dtstandardview_MouseRightButtonUp_1" GridL

我有以下代码的DataGrid

在alternative行背景中有我自己的图像。所以可能的话,我还想改变选定行上的行背景图像

 <DataGrid x:Name="dtstandardview" BorderThickness="0" Height="429"  Width="688" BorderBrush="Aqua" MouseLeftButtonDown="dtstandardview_MouseRightButtonUp_1"
                     GridLinesVisibility="None" MouseRightButtonUp="dtstandardview_MouseRightButtonUp_1" 
                     VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False" IsReadOnly="True" 
                      CanUserDeleteRows="False"  AlternationCount="2" CanUserResizeRows="False" Sorting="dtstandardview_Sorting_1"
                     Background="#DCDCDC" HeadersVisibility="Column" CanUserResizeColumns="False"
                      RowHeight="27" SelectionUnit="FullRow" CanUserAddRows="False" MinRowHeight="27" LoadingRow="dtstandardview_LoadingRow" LoadingRowDetails="dtstandardview_LoadingRowDetails" Loaded="dtstandardview_Loaded" Initialized="dtstandardview_Initialized" CellEditEnding="dtstandardview_CellEditEnding" AutoGeneratingColumn="dtstandardview_AutoGeneratingColumn" UnloadingRow="dtstandardview_UnloadingRow" UnloadingRowDetails="dtstandardview_UnloadingRowDetails"    >

                <DataGrid.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0000"/>
                </DataGrid.Resources>

                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter Property="FontSize" Value="12"/>
                        <Setter Property="FontFamily" Value="Arial"/>
                        <Setter Property="Foreground" Value="#404040"/>
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Setter Property="BorderThickness" Value="0"/>


                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Margin" Value="10,5" />
                        <Setter Property="VerticalContentAlignment" Value="Bottom"/>
                        <Style.Triggers>
                            <Trigger Property="IsSelected"  Value="True">
                                <Setter Property="Background" Value="#cde0e5"/>
                                <Setter Property="FontSize" Value="12"/>
                                <Setter Property="FontFamily" Value="Arial"/>
                                <Setter Property="Foreground" Value="#404040"/>
                                <Setter Property="BorderBrush" Value="Transparent"/>
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Margin" Value="10,5" />
                                <Setter Property="VerticalContentAlignment" Value="Bottom"/>
                            </Trigger>
                        </Style.Triggers>

                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Setter Property="FontSize" Value="12"/>
                        <Setter Property="FontFamily" Value="Arial"/>
                        <Setter Property="Foreground" Value="#404040"/>
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter Property="DataContext">
                            <Setter.Value>
                                <TextBlock Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}">
                                    <TextBlock.Effect>
                                        <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/>
                                    </TextBlock.Effect>
                                </TextBlock>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" >
                                    <Setter.Value>
                                        <ImageBrush ImageSource="/ClientApplication;component/Images/bonus_progress_bg.png"/>

                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.RowBackground >
                    <ImageBrush ImageSource="/ClientApplication;component/Images/second_row_bg.png"/>
                </DataGrid.RowBackground>
                <DataGrid.AlternatingRowBackground>
                    <ImageBrush ImageSource="/ClientApplication;component/Images/bonus_progress_bg.png"/>
                </DataGrid.AlternatingRowBackground>



                <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="VerticalContentAlignment" Value="Center" />
                        <Setter Property="ContentTemplate" >
                            <Setter.Value>
                                <DataTemplate>
                                    <TextBlock Foreground="#404040" FontWeight="Bold" Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}" TextOptions.TextFormattingMode="Display">
                                        <TextBlock.Effect>
                                            <DropShadowEffect BlurRadius="0" Color="#FFFFFF" Direction="-90" Opacity="0.40" ShadowDepth="1"  RenderOptions.ClearTypeHint="Auto" />
                                        </TextBlock.Effect>
                                    </TextBlock>

                                </DataTemplate>
                            </Setter.Value>
                        </Setter>

                        <Setter Property="Background">
                            <Setter.Value>
                                <ImageBrush ImageSource="/ClientApplication;component/Images/table_bg_header.png"/>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush">
                            <Setter.Value>
                                <ImageBrush   ImageSource="/ClientApplication;component/Images/titel_bg.png"/>
                            </Setter.Value>
                        </Setter>

                        <Setter Property="Foreground" Value="#404040" />
                        <Setter Property="BorderThickness" Value="0, 0, 1, 0"/>
                        <Setter Property="Height" Value="26" />
                        <Setter Property="FontSize" Value="14"/>
                        <Setter Property="FontFamily" Value="Arial"/>

                    </Style>
                </DataGrid.ColumnHeaderStyle>

            </DataGrid>

当我选择行时,我在网格上得到以下效果


我想要整排的浅蓝色

您可以使用
模板绑定

<Border Name="DataGridCellBorder"
        Background="{TemplateBinding Background}"/>

以前我也遇到过同样的问题,所以我采用了以下方法,效果很好。一旦尝试它


您正在为网格顶行右侧指定一些宽度(您正在使用按钮)?如果您使用的是Textblock,只需为绑定元素指定相同的宽度即可。

感谢您的快速回复。你能解释一下我在哪里必须这样做吗?虽然这可以回答这个问题,但为了改进你的答案,请提供一个代码示例,以便OP可以与之相关。