【发布时间】: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