Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 从Grid.Row\Column DependencyProperty提取索引_Wpf_Dependency Properties_Multibinding_Imultivalueconverter - Fatal编程技术网

Wpf 从Grid.Row\Column DependencyProperty提取索引

Wpf 从Grid.Row\Column DependencyProperty提取索引,wpf,dependency-properties,multibinding,imultivalueconverter,Wpf,Dependency Properties,Multibinding,Imultivalueconverter,我有以下按钮: <Button Grid.Row="0" Grid.Column="0" Command="{Binding DrawXO}"> <Button.CommandParameter> <MultiBinding Converter="{StaticResource BoardIndexConverter}"> <Binding RelativeS

我有以下按钮:

<Button Grid.Row="0" Grid.Column="0" Command="{Binding DrawXO}">
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource BoardIndexConverter}">
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}" Path="(Grid.Row)"></Binding>
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}" Path="(Grid.Column)" ></Binding>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>
如何从命令中的DependencyProperty获取Grid.Row\Column的实际值

class DrawXOCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        var values = (object[])parameter;
        var row = (int)(values[0]);
        var column = (int)values[1];
    }

    public event EventHandler CanExecuteChanged;

您应该在VisualStudio的输出窗口中看到绑定错误消息,因为按钮在这里不是祖先元素

您应该使用
Self
将按钮用作源对象,而不是
FindAncestor

<MultiBinding Converter="{StaticResource BoardIndexConverter}">
    <Binding RelativeSource="{RelativeSource Self}" Path="(Grid.Row)"/>
    <Binding RelativeSource="{RelativeSource Self}" Path="(Grid.Column)"/>
</MultiBinding>


您的转换器也可能实现得更安全一些:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values.Length == 2 && values[0] is int && values[1] is int)
    {
        return new Tuple<int, int>((int)values[0], (int)values[1]);
    }

    return null;
}
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性) { 如果(values.Length==2&&values[0]为int&&values[1]为int) { 返回新元组((int)值[0],(int)值[1]); } 返回null; } 然后检查命令参数,如下所示:

public void Execute(object parameter)
{
    var cell = parameter as Tuple<int, int>;

    if (cell != null)
    {
        var row = cell.Item1;
        var column = cell.Item2;
        ...
    }
}
public void Execute(对象参数)
{
var cell=参数为元组;
如果(单元格!=null)
{
var行=cell.Item1;
var列=cell.Item2;
...
}
}

So
var值=(object[])参数不起作用?@Clemens我得到一个“DependencyProperty.UnsetValue”数组
public void Execute(object parameter)
{
    var cell = parameter as Tuple<int, int>;

    if (cell != null)
    {
        var row = cell.Item1;
        var column = cell.Item2;
        ...
    }
}