【发布时间】:2015-05-27 15:40:15
【问题描述】:
我有这种风格:
<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 文件中,而不是主窗口中。
【问题讨论】:
-
房产代码?
-
您正在绑定到
Button的DataContext,它将被设置为用于实例化与DataTemplate关联的视图的对象。根据输出,DataTemplate 必须用于字符串。 -
@MatthewFrontino 我在问题中添加了我试图绑定到的视图模型的 ICommand 属性
-
我认为问题不在于命令或数据模板本身(尽管将按钮的内容发送到命令似乎有点奇怪。)。我认为这很可能是数据模板的使用方式。
-
@Xavier,是的,我将 ListBox 绑定到 ObservableCollection
。如何设置此 DataTemplate 以使用带有 Command 的 Button,将 Button 的内容传递给视图模型中的 ICommand 属性?
标签: c# wpf data-binding binding binding-expressions