c# - 如何在 wpf DataGrid 中更改选定行背景图像

标签 c# wpf wpf-controls wpfdatagrid

我有带有以下代码的 DataGrid。

在替代 native 行背景中有我自己的图像。所以有可能然后我也想更改所选行上的行背景图像。

 <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>

当我选择行时,我在网格上获得跟随效果。

enter image description here

我想要浅蓝色的整行。

最佳答案

您可以使用 TemplateBinding

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

关于c# - 如何在 wpf DataGrid 中更改选定行背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14827060/

相关文章:

wpf - 在没有绑定(bind)的情况下通过 DataContext 从 ViewModel 获取值(value)?

c# - 如何使属性知道它所在的属性的名称?

c# - 通过句柄托管 Win32、WinForms 或 WPF 窗口?

c# - 访问 IEnumerable 的属性

c# - 如何让 XAML DataGridColumns 填充整个 DataGrid?

c# - 重构 If 语句

c# - 为什么仅在实现接口(interface)后才重写方法?

c# - Console.Write ("H") 和 Console.Write ('H' ) 在 C# 中有什么区别

c# - 检查 'success' 是否为 null 算作 "Double use of variables"?

c# - 更新绑定(bind)进度条