"Delete" button for RemoveChild not updated

Grids for WPF Forum

Posted 9 years ago by pgenfer
Avatar
Hi,

I have the following issue:

I implemented a CachedPropertyDataAccessorBase to do some special handling in the AddChild and RemoveChild methods. For this reason I've overriden the CanRemove property like this:

public override bool CanRemove
{
     get
     {
        // if internal property can be edited and has a value,
        // enable the user to remove this value
        return !Property.ReadOnly && Property.Value != null;
     }
}
Note: "Property" is just a internal data structure I use.

The problem is, if the first call to "CanRemove" returns true, everything works fine, I can press the "Delete" button and the "RemoveChild" method will be called, after that, the "Delete" button is grayed out until I set the Property.Value again.

But if the first call of "CanRemove" returns false, the "Delete" button will never be visible, no matter if a later call of "CanRemove" will return true. So it seems to me that the "Remove" button visible state is only validated after the first call, if this returns false the button will never be visible again, no matter what is returned by the CanRemove method.

Is there anything I can do to change this behavior or am I doing something wrong with my implementation of the DataAcccessor?

Any help from your side would be appreciated,

Thank you,

Patric

Comments (4)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Patric,

This issue is probably that the Button doesn't know it should call CanExecute and update it's IsEnabled property. If the state of the commands change, then CommandManager.InvalidateRequerySuggested will tell any Buttons (or anything hooked up to commands) to requery CanExecute.

If this is your issue, then you can test it by typing into a TextBox, as that would cause an invalidation.

If that's the case, then you'd need to call CommandManager.InvalidateRequerySuggested when your value changes from non-null to null/read-only to read-write.

Here's a similar question, which also addresses MVVM.


Actipro Software Support

Posted 9 years ago by pgenfer
Avatar
Hi,

thanks for your quick answer.
I tried your suggestion, but unfortunately, it did not change the bevhavior of the "delete" button in the grid, it is still not visible when CanRemove returns false in the first call.
Maybe the problem is that the button is not only disabled at the beginning, but it is not visible at all, so changing the IsEnabled property will not change its visible state?

But I tried a work around yesterday which seems to solve my problem:

I'm checking whether CanRemove is called the first time and always return true, so the button will be initially visible and enabled, in the further calls to CanRemove I then can check my property state and depending on that, the button will be grayed out or enabled.

protected bool m_FirstChecked = false;
        
public override bool CanRemove
{
   get
   {
      if (m_FirstChecked == false)
      {
          m_FirstChecked = true;
          return true;
      }
                
      return !Property.ReadOnly && Property.Value != null;
   }
}
Anyhow I would like to thank you for your quick support.

Patric
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Patric,

The CanRemove is really meant to determine if the delete button is visible. It also happens to determine if the button is enabled/disabled, but really when it's disabled it should be hidden. By default, the remove button is defined like so:
<shared:PopupButton x:Name="removeButton"
        Command="{x:Static propgrid:PropertyGrid.RemovePropertyChildCommand}"
        CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}}"
        Visibility="{Binding CommandParameter.CanRemove, RelativeSource={RelativeSource Self}, Converter={StaticResource BooleanToVisibilityConverter}}">
    ...
</shared:PopupButton>
So when CanRemove is called the "first" time, it determines the Visibility of the button. When you later change CanRemove's value, since you don't raise a PropertyChanged event for "CanRemove", the Visibility binding will never update. If you raised a PropertyChanged event when CanRemove's value is changed, then you'd see the button show/hide.


Actipro Software Support

Posted 9 years ago by pgenfer
Avatar
Hi,

thank you for your suggestion. Calling the OnCanRemoveChanged() (which fires the PropertyChanged) after the Value has changed works perfectly and solved my problem.

Thanks again you for your help, I'm really impressed by the good and quick support you offer your customers.

Greetings,

Patric
The latest build of this product (v2019.1 build 0682) was released 3 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.