简体   繁体   English

如何在 WPF DataGrid 中用透明选择颜色勾勒出所选行?

[英]How to outline the selected row with transparent selection color in WPF DataGrid?

Something that looks like this:看起来像这样的东西:

在此处输入图像描述

I don't want to change the background or foreground color on selection, only outline the selected row.我不想在选择时更改背景或前景色,只勾勒出选定的行。

Is this possible in WPF?这在 WPF 中是否可行? I only see Winforms on SO.我只在 SO 上看到 Winforms。

Ok so I added it to my existing resources, so now my XAML code's resources is like this:好的,所以我将它添加到我现有的资源中,所以现在我的 XAML 代码的资源是这样的:

  <Window.Resources>
        <local:NullImageConverter x:Key="nullImageConverter"/>

        <Style x:Key="DataGridColumnSeparatorStyle" TargetType="DataGridCell">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="#1e90ff"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="DataGridColumnAlarmStyle" TargetType="DataGridCell">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="#000000"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow">
            <Setter Property="Background" Value="#141414"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Style.Triggers>
                <Trigger Property="AlternationIndex" Value="1">
                    <Setter Property="Background" Value="#1e1e1e"/>
                </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderBrush" Value="#1e90ff" />
                        <Setter Property="BorderThickness" Value="1" />
                    </Trigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="DataGridCell">
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Stretch"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Style.Triggers>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value=" "/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=Balance.ProfitPercentageColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Symbol"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=Balance.ProfitPercentageColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="11"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=Balance.ProfitPercentageColor}" />
                        <Setter Property="DataGridCell.Foreground" Value="{Binding Path=Balance.ProfitColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="12"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=Balance.ProfitPercentageColor}" />
                        <Setter Property="DataGridCell.Foreground" Value="{Binding Path=Balance.ProfitColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="13"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=PriceChangeDailyBackColor}" />
                        <Setter Property="DataGridCell.Foreground" Value="{Binding Path=PriceChangeDailyForeColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="14"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=PriceChangeHourlyBackColor}" />
                        <Setter Property="DataGridCell.Foreground" Value="{Binding Path=PriceChangeHourlyForeColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Min %"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Foreground" Value="{Binding Path=PriceChangeMinutelyForeColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Net BTC/m"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=LastMinuteVolumeColor}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{x:Null}"/>
                    <Setter Property="BorderBrush" Value="{x:Null}"/>
                </Trigger>

            </Style.Triggers>
        </Style>

        <Style TargetType="DataGridColumnHeader">
            <Setter Property="HorizontalContentAlignment" Value="Center" />
        </Style>

        <Style TargetType="{x:Type ProgressBar}">
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ProgressBar">
                        <Border BorderThickness="1" Background="#006400" CornerRadius="0" Padding="0">
                            <Grid x:Name="PART_Track">
                                <Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" Fill="#640000" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <CollectionViewSource Source="{Binding Coins}" IsLiveSortingRequested="True" x:Key="MyKey" />

    </Window.Resources>

Final result looks like this:最终结果如下所示: 在此处输入图像描述

So it still uses the background color as set by the alternating colors, and the separator style columns still show up, not sure if there is a way to fix those.所以它仍然使用由交替的 colors 设置的背景颜色,并且分隔样式列仍然显示,不确定是否有办法解决这些问题。

You have modify DataGridRow style.您已修改DataGridRow样式。 Add trigger when IsSelected is true and set BorderBrush and BorderThickness .IsSelected为 true 并设置BorderBrushBorderThickness时添加触发器。

<Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="BorderBrush" Value="Red" />
            <Setter Property="BorderThickness" Value="1" />
        </Trigger>
    </Style.Triggers>
</Style>

Then modify DataGridCell style.然后修改DataGridCell样式。 Add trigger when IsSelected is true and set Background and BorderBrush to Null .IsSelected为 true 时添加触发器,并将BackgroundBorderBrush设置为Null Also set Foreground color otherwise Foreground will be changed when you select a row.还要设置Foreground ,否则当你 select 一行时, Foreground会改变。

    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Foreground" Value="Black"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{x:Null}"/>
                <Setter Property="BorderBrush" Value="{x:Null}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

You can write a style for DataGridRow.您可以为 DataGridRow 编写样式。 something like this.像这样的东西。 you can add this style to App.xaml or resource in the window on anywhere you want.您可以将此样式添加到 App.xaml 或 window 中的资源中您想要的任何位置。

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="BorderBrush" Value="LightGray" />
    <Setter Property="BorderThickness" Value="1" />
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
             <Setter Property="BorderBrush" Value="Red" />
             <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers>
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
    </Style.Resources>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM