WPF绑定到xaml中的多维数组 [英] WPF Binding to multidimensional array in the xaml

查看:86
本文介绍了WPF绑定到xaml中的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用公式表示XAML字符串以链接到多维数组中的特定元素.

I have trouble to formulate the XAML string to link to a specific element in a multidimensional array.

DataContext包含以下几行:

The DataContext contains the following lines:

    private String[] _OneDimension = { "[0]", "[1]" };
    private String[][] _Jagged = { new String[] { "[0,0]", "[0,1]" }, new String[] { "[1,0]", "[1,1]" } };
    private String[,] _TwoDimension = { { "[0,0]", "[0,1]" }, { "[1,0]", "[1,1]" } };

    public String[] OneDimension { get { return _OneDimension; } }
    public String[][] Jagged { get { return _Jagged; } }
    public String[,] TwoDimension { get { return _TwoDimension; } }

XAML包含以下几行:

The XAML contains the following lines:

    <StackPanel>
        <Button Content="{Binding OneDimension[1]}" Width="100" Height="50" />
        <Button Content="{Binding Jagged[1][1]}" Width="100" Height="50" />
        <Button Content="{Binding TwoDimension[1][1]}" Width="100" Height="50" />
    </StackPanel>

OneDimensionJagged的绑定按预期工作.绑定到TwoDimension无效,并且似乎是错误的,但是XAML不允许我使用分隔符,,因此我不知道如何绑定到二维数组.

The binding to OneDimension and Jagged work as expected. The binding to TwoDimension does not work and appears to be wrong, however the XAML does not allow me to use the separator , so I do not know how to bind to a two dimensional array.

此:

        <Button Content="{Binding TwoDimension[1,1]}" Width="100" Height="50" />

不会编译,因为XAML被解释为具有绑定构造函数的两个参数.有没有逃避解析器的方法,或者还有另一种我不知道的写方法?

does not compile because the XAML gets interpreted as having two arguments for the Binding Constructor. Is there some way to escape the parser or is there another way of writing this that I am not aware of?

我刚刚发现可以逃脱这样的分隔符

I just found out that it is possible to escape the separator like this

<Button Content="{Binding TwoDimension[1\,1]}" Width="100" Height="50" />

或仅用这样的标记将参数括起来

or just surround the argument with markers like this

<Button Content="{Binding 'TwoDimension[1,1]'}" Width="100" Height="50" />

但是,此行现在导致异常:System.ArgumentException {"Das Array war kein einDimensiones Array."}不幸的是,C#以我的母语安装了自己-令人讨厌,很糟糕……所以这大致翻译为{"The Array不是一维数组.}

However this line now leads to an exception: System.ArgumentException {"Das Array war kein eindimensionales Array."} unfortunatelly C# installed itself in my native language - annoying as shit... so this roughly translates to {"The Array was not a onedimensionale Array."}

实际上是不可能绑定多维数组的吗?

Is it actually impossible to bind multidimensional arrays?

推荐答案

您可以使用这样定义的MultivalueConverter绑定到二维数组:

You can bind to a Two dimensional array using a MultivalueConverter defined like this :

 class MultiDimensionalCoverter:IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return (values[0] as String[,])[(int) values[1], (int) values[2]];  
    }

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

MultiDimensionalCoverter得到3个参数,两个Dimention数组加上两个索引,而Xaml将像这样:

that MultiDimensionalCoverter get 3 parameters, the Two Dimention array plus the two indexes, and the Xaml will be like this :

<Window.Resources>
        <wpfApp:MultiDimensionalCoverter x:Key="MultiDimensionalCoverter"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Content="{Binding OneDimension[1]}" Width="100" Height="50" />
            <Button Content="{Binding Jagged[1][1]}" Width="100" Height="50" />
            <Button Width="100" Height="50" >
                <Button.Resources>
                    <system:Int32 x:Key="1">1</system:Int32>
                </Button.Resources>
                <Button.Content>
                    <MultiBinding Converter="{StaticResource MultiDimensionalCoverter}">
                        <Binding Path="TwoDimension"/>
                        <Binding Source="{StaticResource 1}"/>
                        <Binding Source="{StaticResource 1}"/>
                    </MultiBinding>
                </Button.Content>
            </Button>
        </StackPanel>
    </Grid>

将索引定义为VM中的属性可能更合适,我仅使用固定值进行演示.

defining the indexes as properties in your VM is probably more appropriate, i am using fixed value only to demonstrate.

这篇关于WPF绑定到xaml中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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