我有这种风格:
<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属性解释为String?
如果它意味着什么,这是在我的App.xaml文件中,而不是主窗口。
答案 0 :(得分:1)
我认为您想要的是让您的绑定在可视树中使用更高的源,其中DataContext
设置为您想要的视图模型,而不是设置为字符串。
例如,假设正确的数据上下文有Grid
,你可以使用这样的绑定:
{Binding DataContext.UninspectedPrintSelectedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}
将Grid
替换为可靠地位于适当级别的可视树中的内容。