0

我有这种风格:

                <DataTemplate>
                    <Button
                        Width="44"
                        Height="24"
                        VerticalAlignment="Top"
                        VerticalContentAlignment="Center"
                        HorizontalAlignment="Left"
                        HorizontalContentAlignment="Center"
                        Command="{Binding UninspectedPrintSelectedCommand}"
                        CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}">

但是该命令无法正常工作。查看输出窗口会产生这个问题:

System.Windows.Data Error: 40 : BindingExpression path error: 'UninspectedPrintSelectedCommand' property not found on 'object' ''String' (HashCode=701007577)'. BindingExpression:Path=UninspectedPrintSelectedCommand; DataItem='String' (HashCode=701007577); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')

ViewModel ICommand 属性:

    public ICommand UninspectedPrintSelectedCommand
    {
        get
        {
            return new DelegateCommand<object>((print) =>
            {
                string printName = print.ToString();
                int indexOfX = printName.IndexOf('x');
                Row = DiePrint.GetRow(printName);
                Col = DiePrint.GetCol(printName);

                if (diePrint == null) { diePrint = new DiePrint(Row + "x" + Col); }
                else
                {
                    diePrint.Row = Convert.ToInt32(row);
                    diePrint.Col = Convert.ToInt32(col);
                }
                LoadMap();
            });
        }
    }

我不知道如何解决这个问题。Button 的 Command 属性如何被解释为字符串?

如果它意味着什么,它在我的 App.xaml 文件中,而不是主窗口中。

4

1 回答 1

1

我认为您想要的是让您的绑定使用视觉树中更高的源,其中DataContext设置为您想要的视图模型,而不是设置为字符串。

例如,假设有一个Grid具有正确数据上下文的更高层,您将使用如下绑定:

{Binding DataContext.UninspectedPrintSelectedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}

替换Grid为在正确级别的可视化树中可靠的内容。

于 2015-05-27T16:01:14.483 回答