clx
clx

Reputation: 824

Hide controls, if their commands shall not be executed

There is an application, which has different permission modes. Depending on the mode, the application has a limited functionality.

Therefore I created my own RestrictedRoutedUICommand class, which inherits from RoutedUICommand and it knows, when it (the command) is allowed to be executed.

This class RestrictedRoutedUICommand has a property Admission, which indicates, if it is allowed to execute the command and an event OnAdmissionChanged which fires, when this property becomes changed.

The Question is: How can I tell those controls, which do have forbidden commands, to hide, if their command is forbidden? It should be similar to the functionality of controls turning disabled, if their command can not be executed.

Edit: I don't want to use the RoutedUICommand.CanExecute(). Because CanExecute() should only determine, if it is possible to execute the command. So I added another method to the RoutedUICommand which determines if the command is allowed, I want the controls to use this one.

Upvotes: 0

Views: 127

Answers (1)

dkozl
dkozl

Reputation: 33364

Since Button already becomes disabled you can make Visibility dependant on IsEnabled property

<Button ...>
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

or you could make use of BooleanToVisibilityConverter to do the same instead of using Trigger

<Button ... Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BooleanToVisibilityConverter}}">

EDIT

If these properties are independent then you still can do it via Binding and Converter

<Button ... Visibility="{Binding RelativeSource={RelativeSource Self}, Path=Command.Admission, FallbackValue=Visible, Converter={StaticResource BooleanToVisibilityConverter}}" 

this code will bind Visiblity to Command.Admission via BooleanToVisibilityConverter converter and if the value cannot be retrieved, because it's not RestrictedRoutedUICommand or Command is not assigned, then FallbackValue will be use

Upvotes: 4

Related Questions