【问题标题】:Prism Commands - binding error when binding to list element?棱镜命令 - 绑定到列表元素时出现绑定错误?
【发布时间】:2010-04-03 17:42:14
【问题描述】:

我有一个 ItemsControl(将被列表框替换),它的 ItemsSource 绑定到位于视图模型中的 ObservableCollection<User>

View Model 包含一些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')..

这意味着存在绑定错误。

有什么办法可以解决这个问题吗?还是不是这样?

【问题讨论】:

    标签: silverlight mvvm prism


    【解决方案1】:

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

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

    <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>
    

    【讨论】:

    • 谢谢,今天大部分时间我都想不出这个
    猜你喜欢
    • 2012-08-16
    • 1970-01-01
    • 2010-12-05
    • 2021-09-07
    • 2020-03-15
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多