Mvvm RadGridView编辑按钮命令在Prism 4.1 Silverlight 5中未绑定
根据我的要求,我在第行和标题行中有一个编辑按钮/超链接,我应该在RadGridView控件中有一个添加新按钮/超链接。这些按钮将加载一个用于添加/编辑数据的子窗口弹出窗口。我的应用程序是在Silverlight5中使用Prism 4.1和MVVM(负责创建视图方法的模型) 我创建了如下按钮:Mvvm RadGridView编辑按钮命令在Prism 4.1 Silverlight 5中未绑定,mvvm,telerik,telerik-grid,prism-4,Mvvm,Telerik,Telerik Grid,Prism 4,根据我的要求,我在第行和标题行中有一个编辑按钮/超链接,我应该在RadGridView控件中有一个添加新按钮/超链接。这些按钮将加载一个用于添加/编辑数据的子窗口弹出窗口。我的应用程序是在Silverlight5中使用Prism 4.1和MVVM(负责创建视图方法的模型) 我创建了如下按钮: <telerik:GridViewColumn Width="80"> <telerik:GridViewColumn.Header>
<telerik:GridViewColumn Width="80">
<telerik:GridViewColumn.Header>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Top"
HorizontalAlignment="Center">
<telerik:RadButton DataContext="{Binding DataContext ,ElementName=LayoutRoot}"
prism:Click.Command="{Binding AddUserEventCommand}"
Content="Add User">
<!-- <Image Source="../../Assets/Images/icon_user_add.png"
Cursor="Hand" Stretch="None" Width="32" Height="32"/>-->
</telerik:RadButton>
</StackPanel>
</telerik:GridViewColumn.Header>
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<telerik:RadButton prism:Click.Command="{Binding DataContext.EditUserEventCommand}">
<Image Source="../../Assets/Images/icon_user_update.png"
Cursor="Hand"
Stretch="None" Width="32" Height="32" >
</Image>
</telerik:RadButton>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
但仍然没有成功。
EditUserEventCommand和AddUserEventCommand都是DelegateCommand
在我的ViewModel中,代码如下所示:
<telerik:RadButton Command="{Binding AddUserEventCommand}" Content="Add User" VerticalAlignment="Top"></telerik:RadButton>
public class UsersTabViewModel : TabViewModelBase , IUsersTabViewModel
{
/// Constructor
public UsersTabViewModel(IUsersTabView view):base(view) {
this.Header = "Users";
this.Title = "Users";
this.IsSelected = true;
ctx = new UserRiaDomainContext();
fillUser();
doEventBinding();
}
private void doEventBinding() {
AddUserEventCommand = new DelegateCommand(AddUser, CanAddUser);
UpdateUserEventCommand = new DelegateCommand(EditUser, CanEditUser);
}
/// Bind with User Add
private DelegateCommand _addUserEventCommand;
public DelegateCommand AddUserEventCommand
{
get { return _addUserEventCommand; }
set
{
_addUserEventCommand = value;
OnPropertyChanged("AddUserEventCommand");
}
}
/// Bind with User Update in Grid Row
private DelegateCommand _updateUserEventCommand;
public DelegateCommand UpdateUserEventCommand {
get { return _updateUserEventCommand; }
set {
_updateUserEventCommand = value;
OnPropertyChanged("UpdateUserEventCommand");
}
}
......
}
如何解决 在越来越多地阅读了有关调用堆栈的内容后,我终于让它工作起来了 由于我的ViewModel和视图绑定是构造函数注入的,所以StaticResource不是我的解决方案。我需要找到祖先数据上下文以获得命令绑定。 所以我喜欢这样:
<telerik:RadButton Command="{Binding DataContext.AddUserEventCommand , RelativeSource={RelativeSource AncestorType=UserControl}}"
Background="Green" Foreground="WhiteSmoke" FontSize="14" Padding="10,5,10,5">
<Image Source="../../Assets/Images/icon_user_add.png"
Cursor="Hand" Stretch="None" Width="32" Height="32"/>
</telerik:RadButton>
这样按钮根据父DataContext绑定,命令开始工作
<telerik:RadButton Command="{Binding DataContext.AddUserEventCommand , RelativeSource={RelativeSource AncestorType=UserControl}}"
Background="Green" Foreground="WhiteSmoke" FontSize="14" Padding="10,5,10,5">
<Image Source="../../Assets/Images/icon_user_add.png"
Cursor="Hand" Stretch="None" Width="32" Height="32"/>
</telerik:RadButton>