【问题标题】:DataGrid Background Binding to Jagged ArrayDataGrid 背景绑定到锯齿状数组
【发布时间】:2013-11-21 20:17:23
【问题描述】:

我有一个数组byte[][] colourMapping,我想操作一个DataGrid,使其绑定到数组(但不是传统意义上的,我相信)。我不想在网格中显示数组的内容,而是想根据数组的值更改特定的单元格背景(我希望单元格本身为空白)。

例如,如果 colourMapping[0][0] == 27,那么我希望 [0][0] 处的数据网格单元格具有某种颜色背景。

此外,随着数组的值发生变化,我希望数据网格的背景也随之变化。

我知道这是一个普遍的问题,但我该怎么做呢?

更新:

这是我目前拥有的。它用字节本身填充数据网格(这是我想避免的),并且似乎没有设置背景:

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();

        //mt.BinaryMapping is a byte[][]
        dataGrid1.ItemsSource = BindingHelper.GetBindable2DViewFromIList<byte>(mt.BinaryMapping);
    }

    private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        DataGridTextColumn column = e.Column as DataGridTextColumn;
        Binding binding = column.Binding as Binding;
        binding.Path = new PropertyPath(binding.Path.Path + ".Value");
    }
}

public class Ref<T>
{
    private readonly Func<T> getter;
    private readonly Action<T> setter;
    public Ref(Func<T> getter, Action<T> setter)
    {
        this.getter = getter;
        this.setter = setter;
    }
    public T Value { get { return getter(); } set { setter(value); } }
}

public class BindingHelper
{
    public static DataView GetBindable2DViewFromIList<T>(IList list2d)
    {
        DataTable dataTable = new DataTable();
        for (int i = 0; i < ((IList)list2d[0]).Count; i++)
        {
            dataTable.Columns.Add(i.ToString(), typeof(Ref<T>));
        }
        for (int i = 0; i < list2d.Count; i++)
        {
            DataRow dataRow = dataTable.NewRow();
            dataTable.Rows.Add(dataRow);
        }
        DataView dataView = new DataView(dataTable);
        for (int i = 0; i < list2d.Count; i++)
        {
            for (int j = 0; j < ((IList)list2d[i]).Count; j++)
            {
                int a = i;
                int b = j;
                Ref<T> refT = new Ref<T>(() => (list2d[a] as IList<T>)[b], z => { (list2d[a] as IList<T>)[b] = z; });
                dataView[i][j] = refT;
            }
        }
        return dataView;
    }
}

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        byte s = (byte)value;
        if (s == 255)
        {
            return Brushes.DeepPink;
        }
        else if (s == 237)
        {
            return Brushes.Blue;
        }
        else
        {
            return Brushes.White;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

这是我的 DataGrid,它显示单元格中的字节(不受欢迎的行为,但易于管理),并且似乎无法正确设置背景颜色:

<DataGrid AutoGenerateColumns="True" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn" HorizontalAlignment="Left" Margin="6,6,0,0" Name="dataGrid1" 
              VerticalAlignment="Top" HeadersVisibility="None" RowHeaderWidth="0" ColumnHeaderHeight="0" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled" 
              VerticalScrollBarVisibility="Disabled" SelectionMode="Extended" SelectionUnit="Cell">
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="Background" Value="{Binding Path=Content, Converter={StaticResource ColorConverter}}" />
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

【问题讨论】:

  • 您是从某个地方开始的,还是要求我们为您完成这项工作?
  • 您需要在后台设置一个 DataGrid.CellStyle,其值为 , Value={Binding Path=. , Converter={StaticResource SomeHexToBrushConverter
  • @devhedgehog 我已经用我目前所掌握的内容更新了我的问题

标签: c# wpf arrays datagrid


【解决方案1】:

您可以使用 CellFormatting 事件设置背景颜色,而无需将数组绑定到网格。

对于我的测试,我使用了 Color 数组而不是字节。

Color[,] colourMapping = new Color[8,9];

 public Form1()
    {
      InitializeComponent();

      for (int i = 0; i < colourMapping.GetLength(0); i++)
      {
        for (int j = 0; j < colourMapping.GetLength(1); j++)
        {
          colourMapping[i,j] = Color.Beige;
        }
      }

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
      if (e.RowIndex < colourMapping.GetLength(0) && e.ColumnIndex < colourMapping.GetLength(1))
      {
        e.CellStyle.BackColor = colourMapping[e.RowIndex, e.ColumnIndex];
      }
    }

【讨论】:

  • 我不相信 CellFormatting 在 WPF 中。
  • 对不起。错过了“wpf”标签。 (原题没有任何代码)
猜你喜欢
  • 2016-11-09
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
相关资源
最近更新 更多