我有一個datagrid,它是一個金融計算器,其中一些單元格在負值上變爲紅色。這是通過DataTrigger與轉換器完成的。我也有覆蓋系統高亮選擇顏色。我現在的問題是,當我選擇一行時,紅色單元格不會突出顯示。WPF數據網格。無法突出顯示自定義樣式的單元格
據我瞭解自定義單元格樣式將覆蓋選擇。我希望我的自定義紅色單元格也可以用0.5不透明藍色條突出顯示。我怎樣才能解決這個問題?
嗯,我可以添加一個額外的觸發器的單元格樣式,選擇後更改BG顏色,並調整顏色以適合,但這是一個kludge。或者,也許我可以在某處實現帶有不透明度的疊加顏色?
<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();
}
}
}
正是我所需要的。公認。 – Disodium