簡體   English   中英

單個單元格的DataGrid更改背景

[英]DataGrid Change Background of a single cell

我試圖在“ DataGrid”中設置單個單元格的背景顏色,但是每次我將顏色設置為DataGridCell時,整個行的背景都會被覆蓋。 我的代碼:

 <Setter Property="CellStyle">
                    <Setter.Value>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Background" Value="{Binding id, Converter={StaticResource TheConverter}}" />
                        </Style>
                    </Setter.Value>
                </Setter>


public class CellHighlighterConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.ToString() == "1") return new SolidColorBrush(Colors.Red);

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

所以問題是... ID為“ 1”的整行都是紅色的! 不只是id單元格。 什么是正確的方法?

您可以嘗試以下解決方法:

  1. 用網格包裝單元格內容。
  2. 將網格設置為水平和垂直拉伸。
  3. 現在,更改網格的背景顏色,而不是單元格的背景顏色。

我還沒有親自測試過,但我認為值得一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM