簡體   English   中英

使用轉換器更改Datagrid單元格顏色

[英]Datagrid cell color change using converter

我試圖通過使用轉換器來更改datagrid單元格顏色,這將使單元格作為參數,因為我將需要單元格和行來選擇單元格的顏色。數據是動態的,因此我沒有任何模型。問題是轉換器在加載數據時不會被擊中。

private void RDataGrid_AutoGeneratedColumns(object sender, EventArgs e)
    {
        foreach (var dataGridColumn in RDataGrid.Columns)
        {
            var textColumn = dataGridColumn as DataGridTextColumn;
            if (textColumn == null) continue;

            textColumn.ElementStyle = FindResource("gridElementStyle") as Style;
            textColumn.EditingElementStyle = FindResource("gridEditElementStyle") as Style;
            textColumn.CellStyle = FindResource("gridCellStyle") as Style;

        }
    }

這是綁定到datagrid的樣式。

 <Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background"
                         Value="{Binding .
                         , RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}
                         ,Converter={StaticResource ColorConverter}   
                            }"  />
</Style>

轉換器:

 public class ColorConverter : IValueConverter
{
     //private string[,] yourarray = new string[100, 100];
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DataGridCell cell = (DataGridCell)value;
        return Brushes.Red;
        //int x = cell.Column.DisplayIndex;
        //var parent = VisualTreeHelper.GetParent(cell);
        //while (parent != null && parent.GetType() != typeof(DataGridRow))
        //{
        //    parent = VisualTreeHelper.GetParent(parent);
        //}
        //int y = ((DataGridRow)parent).GetIndex();

    }

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

這對我有用:

<Style TargetType="DataGridCell">
                                <Setter Property="Foreground" Value="Black"/>
                                <Setter Property="Background">
                                    <Setter.Value>
                                        <MultiBinding Converter="{StaticResource NameToBrushMultiValueConverter}" >
                                            <MultiBinding.Bindings>
                                                <Binding RelativeSource="{RelativeSource AncestorType=Window}" Path="DataContext"/>
                                                <Binding RelativeSource="{RelativeSource AncestorType=DataGrid}"></Binding>
                                                <Binding RelativeSource="{RelativeSource Self}"/>
                                            </MultiBinding.Bindings>
                                        </MultiBinding>
                                    </Setter.Value>
                                </Setter>
                            </Style>

暫無
暫無

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

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