gpt4 book ai didi

silverlight - Prism 命令 - 绑定(bind)到列表元素时出现绑定(bind)错误?

转载 作者:行者123 更新时间:2023-12-03 10:30:55 25 4
gpt4 key购买 nike

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

View 模型包含一些 DelegateCommand<T>处理命令的委托(delegate)(例如 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')..

这意味着存在绑定(bind)错误。

有什么办法可以做到这一点?还是不是这样?

最佳答案

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

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

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

关于silverlight - Prism 命令 - 绑定(bind)到列表元素时出现绑定(bind)错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2572092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com