Prism命令 - 绑定到list元素时绑定错误?

时间:2010-04-03 17:42:14

标签: silverlight mvvm prism

我有一个ItemsControl(由listbox替换),它的ItemsSource绑定到位于视图模型中的ObservableCollection<User>

视图模型包含一些用于处理命令的DelegateCommand<T>个委托(例如UpdateUserCommand和RemoveUserCommand)。如果链接到这些命令的按钮放在显示项目的控件的DataTemplate之外,则一切正常。

<ItemsControl ItemsSource="{Binding Users, Mode=TwoWay}" HorizontalContentAlignment="Stretch"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.2*"/> <ColumnDefinition Width="0.2*"/> <ColumnDefinition Width="0.2*"/> <ColumnDefinition Width="0.2*"/> <ColumnDefinition Width="0.2*"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="{Binding UserName}"/> <PasswordBox Grid.Column="1" Password="{Binding UserPass}"/> <TextBox Grid.Column="2" Text="{Binding UserTypeId}"/> <Button Grid.Column="3" Content="Update" cal:Click.Command="{Binding UpdateUserCommand}" cal:Click.CommandParameter="{Binding}"/> <Button Grid.Column="4" Content="Remove" cal:Click.Command="{Binding RemoveUserCommand}" cal:Click.CommandParameter="{Binding}"/> </Grid>
</DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

我想要实现的目标是:让每一行 - 由ListView / ItemsControl生成 - 包含用于管理表示该特定行的项目的按钮。

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                        

在运行时期间,VS的输出面板为每个列表框元素生成以下消息

System.Windows.Data Error: BindingExpression path error: 'UpdateUserCommand' property not found on 'ModuleAdmin.Services.User' 'ModuleAdmin.Services.User' (HashCode=35912612). BindingExpression: Path='UpdateUserCommand' DataItem='ModuleAdmin.Services.User' (HashCode=35912612); target element is 'System.Windows.Controls.Button' (Name=''); target property is 'Command' (type 'System.Windows.Input.ICommand')..
System.Windows.Data Error: BindingExpression path error: 'RemoveUserCommand' property not found on 'ModuleAdmin.Services.User' 'ModuleAdmin.Services.User' (HashCode=35912612). BindingExpression: Path='RemoveUserCommand' DataItem='ModuleAdmin.Services.User' (HashCode=35912612); target element is 'System.Windows.Controls.Button' (Name=''); target property is 'Command' (type 'System.Windows.Input.ICommand')..

这意味着存在绑定错误。

有没有办法做到这一点?或者这不是这样的?

1 个答案:

答案 0 :(得分:2)

DataTemplate将其DataContext显式设置为模板所代表的项目。也就是说,您的DataTemplate的DataContext将是User对象,而不是包含ObservableCollection的ViewModel。您的命令被绑定的方式,它期望在User对象上找到命令。

您可以显式设置绑定源,也可以覆盖数据上下文。

<ItemsControl HorizontalContentAlignment="Stretch" ItemsSource="{Binding Users, Mode=TwoWay}">
 <ItemsControl.ItemTemplate>
    <DataTemplate>
    ...
      <Button
         Grid.Column="3"
         cal:Click.Command="{Binding UpdateUserCommand, Source={StaticResource myVM}}"
         cal:Click.CommandParameter="{Binding}"
         Content="Update"/>
      <Button
         Grid.Column="4"
         cal:Click.Command="{Binding RemoveUserCommand, Source={StaticResource myVM}}"
         cal:Click.CommandParameter="{Binding}"
         Content="Remove"/>
    ...
    </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>