【问题标题】:RadGridView Edit Button Command Not Binding in Prism 4.1 Silverlight 5RadGridView 编辑按钮命令在 Prism 4.1 Silverlight 5 中未绑定
【发布时间】:2014-07-16 22:00:27
【问题描述】:

根据我的要求,我在行和标题行中有一个编辑按钮/超链接我应该在我的 RadGridView 控件中有一个添加新按钮/超链接。这些按钮将加载一个子窗口弹出窗口以添加/编辑数据。我的应用程序在 Silverlight 5 中,使用 Prism 4.1 和 MVVM(负责创建视图方法的模型)。

我创建了一个这样的按钮:

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

但是我的点击事件不起作用?

当我在 Grid 之外给出相同的 Button 时,它对我有用。 谁的代码是这样的:

   <telerik:RadButton Command="{Binding AddUserEventCommand}" Content="Add User" VerticalAlignment="Top"></telerik:RadButton>

根据几个线程,我发现 DataContext 可能是问题所在。 https://compositewpf.codeplex.com/discussions/64514 所以我尝试通过以下方式更改 Add Button 的 DataContext:

 DataContext="{Binding DataContext ,ElementName=LayoutRoot}"

但仍然没有成功。

EditUserEventCommand 、AddUserEventCommand 都是 DelegateCommand。

在我的 ViewModel 中代码是这样的:

  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");
        }
    }        
     ......
   }

如何解决?

【问题讨论】:

    标签: mvvm telerik telerik-grid prism-4


    【解决方案1】:

    在阅读了更多关于调用堆栈的内容后,我终于让它工作了。

    由于我的 ViewModel 和 View 绑定是构造函数注入的,因此 StaticResource 不是我的解决方案。我需要找到祖先 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>
    

    通过这种方式,Button 根据 Parent DataContext 绑定,并且 Command 开始工作。

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2013-10-09
      • 2010-12-27
      • 1970-01-01
      • 2011-04-13
      • 2011-09-17
      相关资源
      最近更新 更多