需要帮助解决 BindingExpression 问题

Need help resolving a BindingExpression issue

我有这个风格:

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

但是命令不能正常工作。查看输出 window 会产生此问题:

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 的命令 属性 如何被解释为字符串?

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

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

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

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

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