Silverlight:如何在datagrid中触发按钮的单击事件,并使用relay命令将参数传递给事件?

时间:2011-04-11 05:00:43

标签: silverlight silverlight-4.0 mvvm

这是我的xaml和视图模型代码:

 <sdk:DataGrid ItemsSource="{Binding Path=CurrentUserRoles, Mode=TwoWay}" AutoGenerateColumns="False">
                                                <sdk:DataGrid.Columns>
                                                    <sdk:DataGridTemplateColumn Width="80">
                                                        <sdk:DataGridTemplateColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <Button Command="{Binding Path=UsersViewModel.RemoveFromUserRolesCommand}"  Content="Delete" ></Button>
                                                                <!--<Button  Content="Delete" >
                                                                    <i:Interaction.Triggers>
                                                                        <i:EventTrigger EventName="Click">
                                                                            <cmd:EventToCommand Command="{Binding Path=UsersViewModel.RemoveFromUserRolesCommand}"  
                                                                                           CommandParameter="{Binding}" PassEventArgsToCommand="True" 
                                                                        />
                                                                        </i:EventTrigger>
                                                                    </i:Interaction.Triggers>
                                                                </Button>-->
                                                            </DataTemplate>
                                                        </sdk:DataGridTemplateColumn.CellTemplate>
                                                    </sdk:DataGridTemplateColumn>
                                                    <sdk:DataGridTemplateColumn Width="200">
                                                        <sdk:DataGridTemplateColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding Path=Name}" Margin="10,0,0,0"></TextBlock>
                                                            </DataTemplate>
                                                        </sdk:DataGridTemplateColumn.CellTemplate>
                                                    </sdk:DataGridTemplateColumn>
                                                </sdk:DataGrid.Columns>
                                            </sdk:DataGrid>



 private RelayCommand _removeFromUserRolesCommand = null;
        public RelayCommand RemoveFromUserRolesCommand
        {
            get
            {
                if (_removeFromUserRolesCommand == null)
                {

                    _removeFromUserRolesCommand = new RelayCommand(
                        () => this.OnARemoveFromUserRolesCommand(),
                        () => (this._adminModel != null) /*&& IsComboEnabled*/);
                }
                return _removeFromUserRolesCommand;
            }
        }
        private void OnARemoveFromUserRolesCommand()
        {
            try
            {
                if (!_adminModel.IsBusy && SelectedAvailableRole != null)
                {
                   ...
                }
            }
            catch (Exception ex)
            {
               ...
            }
        }

我的问题是网格中的按钮不会触发。如果我把按钮放在网格外面它工作正常。有谁知道上面代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

您的ItemsSource是CurrentUserRoles。如果你的命名约定是正确的,那么按钮将在网格中的DataContext将成为它所在行的角色。除非角色具有UsersViewModel特征,否则你不会去能够挂钩那个命令。

您可能需要提供更多代码来提供上下文,并且您的解决方案可能与绑定到GridI的SelectedItem有关,并且能够绑定到来自内部的命令将删除网格的SelectedItem的角色。

答案 1 :(得分:1)

您的问题是该命令从其模板获取上下文,并且无法访问ViewModel的根目录。将此类添加到您的解决方案中:

public class DataContextProxy : FrameworkElement
    {
        public DataContextProxy()
        {
            this.Loaded += new RoutedEventHandler(DataContextProxyLoaded);
        }

        void DataContextProxyLoaded(object sender, RoutedEventArgs e)
        {
            Binding binding = new Binding();
            if (!String.IsNullOrEmpty(BindingPropertyName))
            {
                binding.Path = new PropertyPath(BindingPropertyName);
            }
            binding.Source = this.DataContext;
            binding.Mode = BindingMode;
            this.SetBinding(DataContextProxy.DataSourceProperty, binding);
        }

        public Object DataSource
        {
            get { return (Object)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null);


        public string BindingPropertyName { get; set; }

        public BindingMode BindingMode { get; set; }

    }

然后像你这样在你的XAML中使用它:

<UserControl.Resources>
        <library:DataContextProxy x:Key="DataContextProxy"/>
</UserControl.Resources>

然后在命令绑定中:

<Button Command="{Binding DataSource.RemoveFromUserRolesCommand, Source={StaticResource DataContextProxy}}" />