【发布时间】:2024-01-02 19:36:01
【问题描述】:
我有一个数据网格,它是一个财务计算器,其中一些单元格在负值时变为红色。这是通过带有转换器的 DataTrigger 完成的。我也有覆盖系统突出显示选择颜色。我现在的问题是,当我选择一行时,红色单元格不会突出显示。
据我了解,自定义单元格样式会覆盖选择。我希望我的自定义红色单元格也用 0.5 不透明度的蓝色条突出显示。我该如何解决这个问题?
好吧,我可以为单元格样式添加一个额外的触发器,该触发器会在选择时更改 BG 颜色并调整颜色以使其适合,但这相当麻烦。或者也许我可以在这里以某种方式实现具有不透明度的叠加颜色?
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
</Trigger>
以下是完整代码。这是一个精简、重量轻但功能齐全的示例。
XAML
<Window.Resources>
<local:ValueToBoolConverter x:Key="ValueToBoolConverter"/>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding MainTable}"
AutoGenerateColumns="False"
CanUserAddRows="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="Azure"/>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" Opacity="0.5" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" />
</Style.Resources>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Income Day" Binding="{Binding IncomeDay}" />
<DataGridTextColumn Header="Income Week" Binding="{Binding IncomeWeek}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding IncomeWeek, Converter={StaticResource ValueToBoolConverter}}" Value="true">
<Setter Property="Background" Value="Salmon"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Income Month" Binding="{Binding IncomeMonth}" />
<DataGridTextColumn Header="Income Year" Binding="{Binding IncomeYear}" Width="*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
C#
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace Datagrid_Cell_Highlight
{
public class TableData
{
public decimal IncomeDay { get; set; }
public decimal IncomeWeek { get; set; }
public decimal IncomeMonth { get; set; }
public decimal IncomeYear { get; set; }
}
public class ViewModel
{
public ObservableCollection<TableData> MainTable { get; set; }
public ViewModel()
{
MainTable = new ObservableCollection<TableData>
{
new TableData { IncomeDay = (decimal)1.11 },
new TableData { IncomeDay = (decimal)2.22 },
new TableData { IncomeDay = (decimal)-1.23 },
new TableData { IncomeDay = (decimal)-2.34 }
};
foreach (var table in MainTable)
{
table.IncomeWeek = table.IncomeDay * 7;
table.IncomeMonth = table.IncomeDay * 30;
table.IncomeYear = table.IncomeDay * 365;
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ValueToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value is decimal) && ((decimal)value < 0))
return true;
else return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
【问题讨论】:
标签: wpf datagrid styles cell highlight