WPF绑定到xaml中的多维数组

Joh*_*nes 9 data-binding wpf binding

我很难将XAML字符串表示为链接到多维数组中的特定元素.

DataContext包含以下行:

    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; } }
Run Code Online (Sandbox Code Playgroud)

XAML包含以下行:

    <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>
Run Code Online (Sandbox Code Playgroud)

结合到OneDimensionJagged正常工作.绑定TwoDimension不起作用,似乎是错误的,但XAML不允许我使用分隔符,,所以我不知道如何绑定到二维数组.

这个:

        <Button Content="{Binding TwoDimension[1,1]}" Width="100" Height="50" />
Run Code Online (Sandbox Code Playgroud)

因为XAML被解释为具有Binding Constructor的两个参数,所以不会编译.有没有办法逃脱解析器或有没有其他方式写这个我不知道?


编辑:

我刚刚发现可以像这样逃离分隔符

<Button Content="{Binding TwoDimension[1\,1]}" Width="100" Height="50" />
Run Code Online (Sandbox Code Playgroud)

或者用这样的标记围绕参数

<Button Content="{Binding 'TwoDimension[1,1]'}" Width="100" Height="50" />
Run Code Online (Sandbox Code Playgroud)

然而,这一行现在导致了一个例外:System.ArgumentException{"Das Array war kein eindimensionales Array."}不幸的是C#以我的母语安装 - 令人讨厌...因此这大致转换为{"The Array不是onedimensionale数组. "}

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

Sam*_*Dev 8

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

 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

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