如何绑定一个二维数组布尔[] []对WPF的DataGrid(单程)? [英] How to bind an 2D array bool[][] to a WPF DataGrid (one-way)?

查看:1006
本文介绍了如何绑定一个二维数组布尔[] []对WPF的DataGrid(单程)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵一种数据网格这样的。

I have a matrix kind datagrid like this.

本格在XAML设计完全

this grid is designed entirely in XAML

现在如何插入值到这些datagridcell与2维数组?
这是需要插入的值必须是布尔的数据类型(TRUE或FALSE)的。
任何想法?

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

推荐答案

下面是我的一个MVVM场景的方法,使用它创建一个数据视图一个转换器,它可以被绑定到网格的ItemsSource 。这对于持有双打特殊矩阵的数据类型,但你可以自己修改它为您的要求:

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:

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();
    }
}

定义为转换器的资源:

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

和使用这样的:

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



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

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

修改

下面是版本的数组布尔[] []:

Here's the version for an array 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();
    }
}

和用法:

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



这是它看起来像在非常原始的版本。然后,您可以将样式DataGrid和编辑它的模板,但这是另外一个问题...

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...

这篇关于如何绑定一个二维数组布尔[] []对WPF的DataGrid(单程)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆