如何将二维数组 bool[][] 绑定到 WPF DataGrid(单向)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16812020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:53:06  来源:igfitidea点击:

How to bind an 2D array bool[][] to a WPF DataGrid (one-way)?

c#wpfwpf-controlswpfdatagrid

提问by Indhi

I have a matrix kind datagrid like this.

我有一个像这样的矩阵类型的数据网格。

enter image description here

在此处输入图片说明

this grid is designed entirely in XAML

这个网格完全是用 XAML 设计的

Now how to insert values into these datagridcell with 2 dimensional array ? the values which is needed to be inserted must be of booldatatype (either TRUE or FALSE). Any ideas ?

现在如何使用二维数组将值插入到这些 datagridcell 中?需要插入的值必须是bool数据类型(TRUE 或 FALSE)。有任何想法吗 ?

回答by Marc

Here is my approach for a MVVM scenario, using a converter which creates a DataView which can be bound to the grids ItemsSource. It's for a special Matrix datatype which holds doubles, but you'll be able to modify it yourself for your requirements:

这是我针对 MVVM 场景的方法,使用转换器创建可以绑定到 grids 的 DataView ItemsSource。它用于一种特殊的 Matrix 数据类型,它包含双打,但您可以根据自己的要求自行修改它:

public class MatrixToDataViewConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var array = value as Matrix;
        if (array == null) return null;

        //var array = ILMath.rand(3, 5);

        var rows = array.Dimensions[0];
        var columns = array.Dimensions[1];

        var t = new DataTable();
        for (var c = 0; c < columns; c++)
        {
            t.Columns.Add(new DataColumn(c.ToString()));
        }

        for (var r = 0; r < rows; r++)
        {
            var newRow = t.NewRow();
            for (var c = 0; c < columns; c++)
            {
                var v = array[r, c];

                // Round if parameter is set
                if (parameter != null)
                {
                    int digits;
                    if (int.TryParse(parameter.ToString(), out digits))
                        v = Math.Round(v, digits);
                }

                newRow[c] = v;
            }

            t.Rows.Add(newRow);
        }


        return t.DefaultView;
    }

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

Define a resource for the converter:

为转换器定义一个资源:

<converter:MatrixToDataViewConverter x:Key="MatrixToDataViewConverter" />

And use it like this:

并像这样使用它:

<DataGrid ItemsSource="{Binding Matrix, Converter={StaticResource MatrixToDataViewConverter}, ConverterParameter=1}"/>

It doesn't allow two way binding, though...

但是它不允许双向绑定...

EDIT

编辑

Here's the version for an array bool[][]:

这是数组 bool[][] 的版本:

public class BoolArrayToDataViewConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var array = value as bool[,];
        if (array == null) return null;

        var rows = array.GetLength(0);
        if (rows == 0) return null;

        var columns = array.GetLength(1);
        if (columns == 0) return null;

        var t = new DataTable();

        // Add columns with name "0", "1", "2", ...
        for (var c = 0; c < columns; c++)
        {
            t.Columns.Add(new DataColumn(c.ToString()));
        }

        // Add data to DataTable
        for (var r = 0; r < rows; r++)
        {
            var newRow = t.NewRow();
            for (var c = 0; c < columns; c++)
            {
                newRow[c] = array[r, c];
            }
            t.Rows.Add(newRow);
        }

        return t.DefaultView;
    }

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

and the usage:

和用法:

<DataGrid ItemsSource="{Binding Matrix, Converter={StaticResource BoolArrayToDataViewConverter}}"/>

And this is what it looks like in the very raw version. You can then style the DataGrid and edit it's templates, but this is another question...

这就是原始版本中的样子。然后您可以设置 DataGrid 的样式并编辑它的模板,但这是另一个问题......

UI

用户界面