This is a migrated thread and some comments may be shown as answers.

Set Binding to CommandProperty of a GridViewCheckBox inside CreateCellElement to ViewModel property

6 Answers 310 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Artur
Top achievements
Rank 1
Artur asked on 20 Nov 2015, 02:59 PM

Hello.

I'am trying to bind ViewModel property to a CommandProperty of a GridViewCheckBox inside of CreateCellElement. Unfortunately the binded command doesn't run while clicking on checkbox. Any ideas why this isn't working?

I'm posting the code where I belive is the mistake. I have made a test Visual Studio 2013  project to try to solve this. It is enable to download here: HERE

Creating  cell element:

internal class GroupDataGridCheckBoxColumn : GridViewCheckBoxColumn
   {
       private readonly Group _group;
 
       public GroupDataGridCheckBoxColumn(Group group)
           : base()
       {
           this._group = group;
           this.AutoSelectOnEdit = true;
           this.EditTriggers = GridViewEditTriggers.CellClick;
       }
 
       public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
       {
           GridViewCheckBox checkBox = base.CreateCellElement(cell, dataItem) as GridViewCheckBox;
 
           /// <THIS DOESNT WORK>
 
           /* Set Command binding */
           Binding commandBinding = new Binding("DataContext.AddOrRemoveGroupCommand");
           commandBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1);
           checkBox.SetBinding(CheckBox.CommandProperty, commandBinding);
 
           /* Set Command parameter */
           MultiBinding commandParameterBinding = new MultiBinding();
           commandParameterBinding.Converter = new CommandParameterMultiConverter();
           commandParameterBinding.Bindings.Add(new Binding("IsChecked") { RelativeSource = RelativeSource.Self });
           commandParameterBinding.Bindings.Add(new Binding(".")); //the employee object
           commandParameterBinding.Bindings.Add(new Binding(".") { Source = this._group }); //the group object
           checkBox.SetBinding(CheckBox.CommandParameterProperty, commandParameterBinding);
 
           /// <THIS DOESNT WORK/>
 
           return checkBox;
       }
   }

 

Any ideas? This error consumed 1 day of my work for now..

6 Answers, 1 is accepted

Sort by
0
Artur
Top achievements
Rank 1
answered on 23 Nov 2015, 07:51 AM

What I'm trying to do is described HERE
I have made it using standard windows DataGrid and it works. When I'm switching to telerik controls and changing some equivalent property names it doesn't fire AddORemoveGroupCommand command while clicking the combobox.

0
Stefan Nenchev
Telerik team
answered on 25 Nov 2015, 02:52 PM
Hi Artur,

I have checked the project you have provided and the issue you are experiencing seems to be related to the way the DataContext for the CommandBinding is set.  Please, test with the following approach:
var grid = cell.ParentOfType<RadGridView>();
Binding commandBinding = new Binding("AddOrRemoveGroupCommand") { Source = grid.DataContext};

Furthermore, you need to change the method you are overriding to CreateCellEditElement as the RadGridView enters edit mode when checking/unchecking the box:

public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
{
...
}

Artur, please test the aforementioned approach and update me if it suits your needs. 

Regards,
Stefan Nenchev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Artur
Top achievements
Rank 1
answered on 26 Nov 2015, 10:48 AM

I have made the changes that you suggested and changed the called base method to base.CreateCellElement. So the modified method looks like this:

public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
{
    CheckBox checkBox = base.CreateCellEditElement(cell, dataItem) as CheckBox;
  
    /* Set Command binding */
    var grid = cell.ParentOfType<RadGridView>();
    Binding commandBinding = new Binding("AddOrRemoveGroupCommand") { Source = grid.DataContext };
    checkBox.SetBinding(CheckBox.CommandProperty, commandBinding);
  
    /* Set Command parameter */
    /*(...)*/
  
    return checkBox;
}

It helped. Now the method AddOrRemoveGroup in ViewModel is fired when you select/deselect the grid checkboxes so I can handle groups and employees data in ViewModel.

 But...

It lead to two issues.

First: When you click for the first time on a checkbox the AddOrRemoveGroup isn't fired. So in ViewModel we lost the one change of the checkbox value. I supose it's becouse while clicking for the first time the cell isn't yet in EditMode. We can bound a command in CreateCellElement, but this isn't working as we could see in the first post. Also we can handle it by changeing

this.AutoSelectOnEdit = false;
So the first click is only entering editmode without selecting/deselecting the checkbox. Still it isn't the solution becouse I would like to change the checkbox value with one click.

 Second: After leaving the changed checkbox the ConvertBack method from GroupsToBooleanConverter is fired. As it throws NotSupportedException it makes unable to leave the current edited checkbox.

 

Is there a way to fire AddOrRemoveGroup also after the first checkbox click? How can I diseable triggering ConvertBack from GroupsToBooleanConverter?

 

0
Stefan Nenchev
Telerik team
answered on 27 Nov 2015, 09:31 AM
Hi Artur,

I have managed to achieve the desired behavior by making a mixture between overriding the CreateCellElement and calling the base.CreateCellEditElement in it, as in the code snippet below:

public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
      {
          CheckBox checkBox = base.CreateCellEditElement(cell, dataItem) as CheckBox;
 
          /* Set Command binding */
          var grid = cell.ParentOfType<RadGridView>();
          Binding commandBinding = new Binding("AddOrRemoveGroupCommand") { Source = grid.DataContext };
          checkBox.SetBinding(CheckBox.CommandProperty, commandBinding);
 
          /* Set Command parameter */
          MultiBinding commandParameterBinding = new MultiBinding();
          commandParameterBinding.Converter = new CommandParameterMultiConverter();
          commandParameterBinding.Bindings.Add(new Binding("IsChecked") { RelativeSource = RelativeSource.Self });
          commandParameterBinding.Bindings.Add(new Binding(".")); //the employee object
          commandParameterBinding.Bindings.Add(new Binding(".") { Source = this._group }); //the group object
          checkBox.SetBinding(CheckBox.CommandParameterProperty, commandParameterBinding);
          /*(...)*/
 
          return checkBox;
      }

I believe this is the result you aim for. Please, test it and update me if it works as expected.

Regards,
Stefan Nenchev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Artur
Top achievements
Rank 1
answered on 27 Nov 2015, 10:15 AM

Hi!

Thanks a lot! This is it. It's working now. Still not quite how I expected. Sometimes after clicking in a cell, but not on the comboBox it enteres editMode for the cell resulting trigerring ConvertBack when clicking on another cell. I managed to fix it by myself setting

base.IsReadOnly = true;

the cell element to read only.

 

So the complete CreateCellElement method looks like this:

public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
{
    base.IsReadOnly = true;
    CheckBox checkBox = base.CreateCellEditElement(cell, dataItem) as CheckBox;
 
    /* Set Command binding */
    var grid = cell.ParentOfType<RadGridView>();
    Binding commandBinding = new Binding("AddOrRemoveGroupCommand") { Source = grid.DataContext };
    checkBox.SetBinding(CheckBox.CommandProperty, commandBinding);
 
    /* Set Command parameter */
    MultiBinding commandParameterBinding = new MultiBinding();
    commandParameterBinding.Converter = new CommandParameterMultiConverter();
    commandParameterBinding.Bindings.Add(new Binding("IsChecked") { RelativeSource = RelativeSource.Self });
    commandParameterBinding.Bindings.Add(new Binding(".")); //the employee object
    commandParameterBinding.Bindings.Add(new Binding(".") { Source = this._group }); //the group object
    checkBox.SetBinding(CheckBox.CommandParameterProperty, commandParameterBinding);
 
    return checkBox;
}

 

Thanks for support!

0
Stefan Nenchev
Telerik team
answered on 27 Nov 2015, 12:45 PM
Hello Artur,

I am glad to see that I was of help and you have managed to achieve the desired behavior. 

Regards,
Stefan Nenchev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Artur
Top achievements
Rank 1
Answers by
Artur
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or