如何更改DataGrid选定的行颜色WPF

时间:2018-09-10 06:25:29

标签: c# wpf xaml datagrid resourcedictionary

我已经在WPF应用程序中创建了一个数据网格。我想更改选定的行颜色。我有以下代码

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:navigationApp.Resources">

    <Style x:Key="DataGridColumnHeaderGripper" TargetType="Thumb">
        <Setter Property="Width" Value="18"/>
        <Setter Property="Background" Value="#252526"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Padding="{TemplateBinding Padding}" Background="Transparent" BorderBrush="#3e3e45">
                        <Rectangle HorizontalAlignment="Center" Width="1" Fill="{TemplateBinding Background}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Padding" Value="8,5" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="Transparent">
                        <ContentPresenter
                            Margin="{TemplateBinding Padding}"
                            Content="{TemplateBinding ContentControl.Content}"
                            ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                            ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                            SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Margin" Value="0"/>
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Grid>
                        <Border x:Name="BorderOutline" BorderThickness="2,1,1,1" />
                        <Border x:Name="BorderInline" BorderThickness="0" />
                        <Grid Background="Black" Opacity="0" />
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter
                                ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}"
                                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                Grid.Column="1" />
                            <DataGridDetailsPresenter
                                Visibility="{TemplateBinding DataGridRow.DetailsVisibility}"
                                Grid.Column="1"
                                Grid.Row="1"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Both" />
                            <DataGridRowHeader
                                Visibility="Visible"
                                Grid.RowSpan="2"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />
                        </SelectiveScrollingGrid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsSelected" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="{DynamicResource ApplicationAccentBrush}" TargetName="BorderOutline" />
                            <Setter Property="Opacity" Value="0.8" TargetName="BorderOutline" />
                            <Setter Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}" TargetName="BorderInline" />
                            <Setter Property="Opacity" Value="0.2" TargetName="BorderInline" />
                        </MultiTrigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="BorderBrush" Value="{DynamicResource ApplicationAccentBrush}" TargetName="BorderOutline" />
                            <Setter Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}" TargetName="BorderInline" />
                            <Setter Property="Opacity" Value="0.3" TargetName="BorderInline" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Padding" Value="8,2,8,2"/>
        <Setter Property="Background" Value="#252526"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Border Name="HeaderBorder" Padding="{TemplateBinding Padding}" BorderThickness="1,1,1,1" BorderBrush="#3e3e45" Background="#252526">
                            <ContentPresenter
                                Name="HeaderContent"
                                Margin="0,0,0,1"
                                RecognizesAccessKey="True"
                                Content="{TemplateBinding ContentControl.Content}"
                                ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                                HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </Border>
                        <!--<Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0,0,-9,0" Style="{StaticResource DataGridColumnHeaderGripper}"/>-->
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="HeaderBorder" Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="HeaderBorder" Property="Background" Value="{DynamicResource ApplicationAccentBrush}"/>
                            <Setter TargetName="HeaderContent" Property="Margin" Value="1,1,0,0"/>
                        </Trigger>
                        <Trigger Property="Selector.IsSelected" Value="True">
                            <Setter Property="TextElement.Foreground" Value="White"/>
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

当前,选定的行颜色是白色或透明的。所以我看不到所选的行详细信息。

2 个答案:

答案 0 :(得分:2)

您可以使用触发器更改所选行的颜色。如数据网格样式所示。选择行时,将颜色设置为父控件的背景属性(即Grid作为TargetName)。

<Style TargetType="{x:Type DataGridRow}">       
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Grid x:Name="selectedRow">                        
                            <DataGridCellsPresenter
                                ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}"
                                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                Grid.Column="1" />
                            <DataGridDetailsPresenter
                                Visibility="{TemplateBinding DataGridRow.DetailsVisibility}"
                                Grid.Column="1"
                                Grid.Row="1"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Both" />
                            <DataGridRowHeader
                                Visibility="Visible"
                                Grid.RowSpan="2"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />
                        </SelectiveScrollingGrid>
                    </Grid>
                    <ControlTemplate.Triggers>                                                        
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="{DynamicResource ApplicationAccentBrushSecondary}" TargetName="selectedRow" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

答案 1 :(得分:0)

在我的项目中,我做了如下操作:

lapply

//let amount = "123,456,789.545222323" //let amount = "123,456,789." let amount = "123,456,789" let removeSpaces = amount.replacingOccurrences(of: " ", with: "") if removeSpaces.count > 0 { let arrSTR = removeSpaces.components(separatedBy: ".") if arrSTR.count > 1 { var strAfterDecimal = arrSTR[1] if strAfterDecimal.count >= 2 { strAfterDecimal = strAfterDecimal[0..<2] }else if strAfterDecimal.count != 0 { strAfterDecimal = "\(strAfterDecimal)0" }else { strAfterDecimal = "00" } let finalSTR = String("\(arrSTR[0]).\(strAfterDecimal)") print("Final with Decimal - \(finalSTR)") }else { let finalSTR = String(arrSTR[0] + ".00") print("Final without Decimal - \(finalSTR)") } } extension String { subscript(_ range: CountableRange<Int>) -> String { let idx1 = index(startIndex, offsetBy: max(0, range.lowerBound)) let idx2 = index(startIndex, offsetBy: min(self.count, range.upperBound)) return String(self[idx1..<idx2]) } } <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource DataGridCell}"> <Setter Property="Background" Value="{StaticResource DataGridCellBackgroundBrush}"/> <Setter Property="BorderBrush" Value="{StaticResource DataGridBorderBrush}" /> <Setter Property="BorderThickness" Value="0"/> <Style.Triggers> <!-- IsSelected --> <Trigger Property="IsSelected" Value="True"> <Setter Property="Foreground" Value="{StaticResource BlackBrush}" /> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Controls:DataGridCellHelper.IsCellOrRowHeader" Value="True" /> <Condition Property="IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="{StaticResource DataGridCellBackgroundBrush}" /> <Setter Property="BorderBrush" Value="{StaticResource DataGridCellBackgroundBrush}" /> </MultiTrigger> //others properties 是我预定义的颜色

相关问题