【问题标题】:How to set transparent background for the selected data grid row?如何为选定的数据网格行设置透明背景?
【发布时间】:2021-06-08 12:42:18
【问题描述】:

如果我使用像红色这样的普通颜色,它会起作用。但是如果我使用透明,它在未聚焦时显示为白色,在聚焦时显示为蓝色。我必须为窗口启用特殊设置吗?

这是最少的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace test
{
    public partial class MainWindow : Window
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void SetField<T> ( ref T field, T value, string propertyName )
        {
            if ( !EqualityComparer<T>.Default.Equals ( field, value ) )
            {
                field = value;
                PropertyChanged?.Invoke ( this, new PropertyChangedEventArgs ( propertyName ) );
            }
        }

        ObservableCollection<Coin> _coins;
        public ObservableCollection<Coin> Coins { get => _coins; set => SetField ( ref _coins, value, nameof ( _coins ) ); }
        public ICollectionView CollectionView;

        public MainWindow ( )
        {
            this.Coins = new ObservableCollection<Coin> ( );
            this.Coins.Add ( new Coin ( "Coin 1", 1 ) );
            this.Coins.Add ( new Coin ( "Coin 2", 2 ) );
            this.Coins.Add ( new Coin ( "Coin 3", 3 ) );
            this.Coins.Add ( new Coin ( "Coin 4", 4 ) );
            this.Coins.Add ( new Coin ( "Coin 5", 5 ) );
            this.Coins.Add ( new Coin ( "Coin 5", 6 ) );
            this.Coins.Add ( new Coin ( "Coin 5", 7 ) );
            this.Coins.Add ( new Coin ( "Coin 5", 8 ) );
            this.Coins.Add ( new Coin ( "Coin 5", 9 ) );
            this.Coins.Add ( new Coin ( "Coin 5", 10 ) );
            this.Coins.Add ( new Coin ( "Coin 5", 11 ) );

            this.DataContext = this;

            InitializeComponent ( );
        }
    }

    public class Coin
    {
        public string Symbol { get; set; }
        public int PNL { get; set; }
        public SolidColorBrush Color2 { get; set; }

        public Coin ( string symbol, int pnl )
        {
            this.Symbol = symbol;
            this.PNL = pnl;

            Random rnd = new Random ( );
            Color c = Color.FromRgb ( ( byte ) rnd.Next ( 256 ), ( byte ) rnd.Next ( 256 ), ( byte ) rnd.Next ( 256 ) );

            this.Color2 = new SolidColorBrush ( c );
        }
    }
}

Xaml:

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:test"
    mc:Ignorable="d"
    Name="myMainWindow"
    SizeToContent="Width"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="Profit Tracker"
    WindowStyle="None"
    AllowsTransparency="True"
    Topmost="True"
    ResizeMode="NoResize"
    Height="426">

    <Window.Resources>

        <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 TargetType="{x:Type DataGrid}">
            <Setter Property="Background" Value="#FFF" />
            <Setter Property="AlternationCount" Value="2" />
            <Setter Property="BorderBrush" Value="Blue" />
            <Setter Property="BorderThickness" Value="0" />
        </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="0">
                    <Setter Property="Background" Value="#141414"/>
                </Trigger>
                <Trigger Property="AlternationIndex" Value="1">
                    <Setter Property="Background" Value="#282828"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderBrush" Value="#1e90ff" />
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Margin" Value="-1,0,0,0" />
                    <!--<Setter Property="Background" Value="Transparent"/>-->
                </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"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />

            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderBrush" Value="#1e90ff" />
                    <Setter Property="BorderThickness" Value="1" />
                    <!--<Setter Property="Background" Value="Red"/>-->
                </Trigger>

                <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.Header, RelativeSource={RelativeSource Self}}" Value="PNL %"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="DataGridCell.Background" Value="{Binding Path=Color2}" />
                        <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="#75001D" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

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

    </Window.Resources>

    <Grid>
        <DataGrid Name="dataGrid" ItemsSource="{Binding Source={StaticResource MyKey}}" SelectionMode="Single" GridLinesVisibility="None" HorizontalScrollBarVisibility="Hidden" RowHeaderWidth="0" IsReadOnly="True" CanUserAddRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" AutoGenerateColumns="False" RowStyle="{StaticResource RowStyleWithAlternation}">
            <DataGrid.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            </DataGrid.Resources>

            <DataGrid.Columns>

                <DataGridTemplateColumn Header=" " Visibility="Hidden" MinWidth="0" Width="10" CanUserSort="False"/>

                <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}"/>
                <DataGridTextColumn Header="PNL" Width="60" SortMemberPath="Balance.UnitPrice" Binding="{Binding Path=PNL}" />

                <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}" CanUserSort="False"/>

                <DataGridTextColumn Width="60" SortMemberPath="Balance.Profit" Binding="{Binding Path=Balance.ProfitDisplay}">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="{Binding DataContext.Market.TotalProfitInUSDDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <DataGridTextColumn Header="PNL %" Width="60" SortMemberPath="Balance.ProfitPercentage" Binding="{Binding Path=Balance.ProfitPercentageDisplay}"/>

                <DataGridTextColumn Width="60" Binding="{Binding Path=PriceChangeInPercentDailyDisplay}">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="{Binding DataContext.Market.PriceChangeInPercentDailyDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <DataGridTextColumn Width="60" SortMemberPath="PriceChangeInPercentHourly" Binding="{Binding Path=PriceChangeInPercentHourlyDisplay}">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="{Binding DataContext.Market.PriceChangeInPercentHourlyDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}" CanUserSort="False"/>

                <DataGridTextColumn Width="60" SortMemberPath="PriceChangeInPercentMinutely" Binding="{Binding Path=PriceChangeInPercentMinutelyDisplay}">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="{Binding DataContext.Market.PriceChangeInPercentMinutelyDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <DataGridTemplateColumn Header="Vol BTC/h" Width="60" SortMemberPath="LastHourVolumeInBtc">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <ProgressBar Value="{Binding Path=TotalSellVolumePercentage, Mode=OneWay}" Minimum="0" Maximum="1"/>
                                <TextBlock Text="{Binding Path=LastHourVolumeInBtcDisplay}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Net BTC/m" Width="60" SortMemberPath="LastMinuteVolumeInBtc" Binding="{Binding Path=LastMinuteVolumeInBtcDisplay}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

【问题讨论】:

  • 会不会是窗口背景造成的?您能否将窗口的背景设置为红色或透明,看看这是否会影响您看到的内容。
  • @XAMlMAX 这个我刚试过,还是一样,不知道怎么回事。
  • 所以你的主要问题是什么时候没有被选中或者什么时候被选中?不管怎样,这看起来像是DataGridRow 风格的问题。我建议在 Blend 中编辑并覆盖模板。这可能与 Button 的问题相同。那里有一个不打球的边界。试一试,看看会发生什么。
  • @XAMlMAX 被选中时。当它没有被选中时,它看起来是正确的。当窗口不在焦点时,它也会改变选择颜色,所以我也必须改变它。我不想让焦点改变选择颜色。
  • 我明白了,我会看看并尝试重现这个(现在是上午 10 点我所在的位置),今天下午。我认为一旦你覆盖了模板,它就会修复它。

标签: c# .net wpf datagrid styling


【解决方案1】:

如果你正在寻找这样的东西:

我通过在 DataGridCell 的属性中添加透明背景并在 IsSelected 触发器上从 DataGridRow 中删除黄色背景来做到这一点

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:test"
    mc:Ignorable="d"
    SizeToContent="Width"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="test"
    WindowStyle="None"
    AllowsTransparency="True"
    Topmost="True"
    Height="426">

<Window.Resources>

    <Style x:Key="DataGridColumnSeparatorStyle" TargetType="DataGridCell">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="Red"/>
                </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 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"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Background" Value="Transparent"/>

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

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

    <Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow">
        <Setter Property="Background" Value="DarkKhaki"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderBrush" Value="DarkMagenta" />
                <Setter Property="BorderThickness" Value="2" />
            </Trigger>
        </Style.Triggers>
    </Style>

</Window.Resources>

<Grid>
    <DataGrid Name="dataGrid" ItemsSource="{Binding Coins}" 
              SelectionMode="Single" GridLinesVisibility="None" 
              HorizontalScrollBarVisibility="Hidden" 
              RowHeaderWidth="0" IsReadOnly="True" 
              CanUserAddRows="False" CanUserResizeColumns="False" 
              CanUserResizeRows="False" AutoGenerateColumns="False" 
              RowStyle="{StaticResource RowStyleWithAlternation}">
        <DataGrid.Resources>
        </DataGrid.Resources>

        <DataGrid.Columns>

            <DataGridTemplateColumn Header=" " MinWidth="0" Width="10" CanUserSort="False"/>

            <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}"/>
            <DataGridTextColumn Header="Symbol" Width="64" Binding="{Binding Path=Symbol}" />

            <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}" CanUserSort="False"/>

            <DataGridTextColumn Header="PNL %" Width="64" Binding="{Binding Path=Color}"/>

            <DataGridTextColumn Width="64" Binding="{Binding Path=PriceChangeInPercentDailyDisplay}">
                <DataGridTextColumn.Header>
                    <TextBlock Text="Profit"/>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid></Window>

更新

问题中更新后,我刚刚删除了

&lt;Setter Property="Background" Value="Transparent"/&gt;来自

&lt;Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow"&gt; 在原始代码中粘贴在问题本身中,这就是网格对我的看法

这是你要找的还是我错过了什么?

编辑

完整的 MainWindow.xaml 代码

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:test"
    mc:Ignorable="d"
    Name="myMainWindow"
    SizeToContent="Width"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="Profit Tracker"
    WindowStyle="None"
    AllowsTransparency="True"
    Topmost="True"
    ResizeMode="NoResize"
    Height="426">

<Window.Resources>

    <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 TargetType="{x:Type DataGrid}">
        <Setter Property="Background" Value="#FFF" />
        <Setter Property="AlternationCount" Value="2" />
        <Setter Property="BorderBrush" Value="Blue" />
        <Setter Property="BorderThickness" Value="0" />
    </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="0">
                <Setter Property="Background" Value="#141414"/>
            </Trigger>
            <Trigger Property="AlternationIndex" Value="1">
                <Setter Property="Background" Value="#282828"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderBrush" Value="#1e90ff" />
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="Margin" Value="-1,0,0,0" />
                <!--<Setter Property="Background" Value="Transparent"/>-->
            </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"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderBrush" Value="#1e90ff" />
                <Setter Property="BorderThickness" Value="1" />
                <!--<Setter Property="Background" Value="Red"/>-->
            </Trigger>

            <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="#75001D" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

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

</Window.Resources>

<Grid>
    <DataGrid Name="dataGrid" ItemsSource="{Binding Source={StaticResource MyKey}}" SelectionMode="Single" GridLinesVisibility="None" HorizontalScrollBarVisibility="Hidden" RowHeaderWidth="0" IsReadOnly="True" CanUserAddRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" AutoGenerateColumns="False" RowStyle="{StaticResource RowStyleWithAlternation}">
        <DataGrid.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        </DataGrid.Resources>

        <DataGrid.Columns>

            <DataGridTemplateColumn Header=" " Visibility="Hidden" MinWidth="0" Width="10" CanUserSort="False"/>

            <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}"/>
            <DataGridTextColumn Header="PNL" Width="60" SortMemberPath="Balance.UnitPrice" Binding="{Binding Path=PNL}" />

            <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}" CanUserSort="False"/>

            <DataGridTextColumn Width="60" SortMemberPath="Balance.Profit" Binding="{Binding Path=Balance.ProfitDisplay}">
                <DataGridTextColumn.Header>
                    <TextBlock Text="{Binding DataContext.Market.TotalProfitInUSDDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>

            <DataGridTextColumn Header="PNL %" Width="60" SortMemberPath="Balance.ProfitPercentage" Binding="{Binding Path=Balance.ProfitPercentageDisplay}"/>

            <DataGridTextColumn Width="60" Binding="{Binding Path=PriceChangeInPercentDailyDisplay}">
                <DataGridTextColumn.Header>
                    <TextBlock Text="{Binding DataContext.Market.PriceChangeInPercentDailyDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>

            <DataGridTextColumn Width="60" SortMemberPath="PriceChangeInPercentHourly" Binding="{Binding Path=PriceChangeInPercentHourlyDisplay}">
                <DataGridTextColumn.Header>
                    <TextBlock Text="{Binding DataContext.Market.PriceChangeInPercentHourlyDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>

            <DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}" CanUserSort="False"/>

            <DataGridTextColumn Width="60" SortMemberPath="PriceChangeInPercentMinutely" Binding="{Binding Path=PriceChangeInPercentMinutelyDisplay}">
                <DataGridTextColumn.Header>
                    <TextBlock Text="{Binding DataContext.Market.PriceChangeInPercentMinutelyDisplay, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>

            <DataGridTemplateColumn Header="Vol BTC/h" Width="60" SortMemberPath="LastHourVolumeInBtc">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ProgressBar Value="{Binding Path=TotalSellVolumePercentage, Mode=OneWay}" Minimum="0" Maximum="1"/>
                            <TextBlock Text="{Binding Path=LastHourVolumeInBtcDisplay}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Net BTC/m" Width="60" SortMemberPath="LastMinuteVolumeInBtc" Binding="{Binding Path=LastMinuteVolumeInBtcDisplay}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid></Window>

【讨论】:

  • 谢谢我刚刚尝试了交替行颜色,它似乎不起作用。你能试一试吗?我更新了 OP。
  • 嘿,我尝试使用新代码并更新了我的答案。您能否看一下屏幕截图,如果您希望网格看起来像这样,请告诉我?
  • 非常感谢,当我复制粘贴您的代码时,它不起作用。是否可以将您的更改添加到我的代码中,以便我可以复制粘贴并区分更改?否则很难跟上变化,对我来说更容易出错。
  • 贴出完整代码。它有两行被注释掉并且对我来说工作正常。我还注意到,当网格不在焦点时(第一个单元格中的数字变为黑色),您选择的网格行的行为会有所不同。我不知道你是否也想解决这个问题,但可以查看stackoverflow.com/questions/40710351/…。希望这会有所帮助!
  • 非常感谢它有效!顺便说一句,为什么选定的行看起来也发生了变化?是否也可以解决这个问题?我发布了关于它但没有回复。
【解决方案2】:

您制作了所有内容 - DataGridRow.Background、DataGridCell.Background - 透明

选择后,您可以看到白色 DataGrid 背景 (#FFF) - 通过行和单元格

要解决此问题,请在 IsSelected 触发器中删除背景设置器:

<Trigger Property="IsSelected" Value="True">
    <Setter Property="BorderBrush" Value="#1e90ff" />
    <Setter Property="BorderThickness" Value="1" />
    <!--<Setter Property="Background" Value="Transparent"/>-->
</Trigger>

它将确保以前的触发器之一有效:

<Trigger Property="AlternationIndex" Value="0">
    <Setter Property="Background" Value="#141414"/>
</Trigger>
<Trigger Property="AlternationIndex" Value="1">
    <Setter Property="Background" Value="#282828"/>
</Trigger>

【讨论】:

  • 谢谢我试过了,但它不起作用。实际上我不得不更改 OP 代码,因为在某些单元格上还会发生另一种背景颜色。
  • 所以不确定为什么选择使用备用背景颜色,但如果没有选择,则使用单个单元格背景。
  • @JoanVenge,另一个 bg 仅适用于一列。在&lt;Style TargetType="DataGridCell"&gt;中尝试&lt;Setter Property="Background" Value="Transparent" /&gt;
  • 谢谢,我刚试过,但它的颜色与您的示例中删除透明 bg 的颜色相同。
猜你喜欢
  • 2010-11-22
  • 2012-11-24
  • 2011-10-04
  • 2019-03-18
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多