开发者

Silverlight: How to trigger a click event of a button inside a datagrid and pass a parameter to the event using relay command?

here is my xaml and view model code:

 <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)
            {
               ...
            }
        }

My problem here is that the button in the grid wont trigger. If i put the button outside the grid it works fine. Anybody knew what went wrong with the code above?


Your ItemsSource is CurrentUserRoles. If your naming conventions are correct, then the DataContext that the button will be in within the grid is going to be the role of the row that it is in. Unless a role has a UsersViewModel characteristic, you're not going to be able to hook into that command.

You're probably going to need to provide a little more code to give context, and your solution is probably going to have something to do with binding to SelectedItem for the grid, and being able to bind to a command from within the role that will delete the SelectedItem for the grid.


your problem is that the command is getting the context from its template and there it cannot access the root of the ViewModel. Add this class to your solution:

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

    }

then use it in you XAML like so:

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

Then in your command binding:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜