In my application I want to show/hide a button according to the users authorization level.
If the user is a team leader the button should be shown. If the user is not a team leader it shouldn’t be shown.
I tried to resolve this issue using a BooleanToVisibilityConverter which is defined in my resource dictionary:
<BooleanToVisibilityConverter x:Key="VisibilityConverter" />
Implementation of the converter:
<Button Grid.Row="1" Grid.Column="5"
Click="TeamLeader_Click" Visibility="{Binding IsTeamLeader, Converter={StaticResource
VisibilityConverter}}" Style="{StaticResource ButtonStyleMenu}" />
In my code behind class I’m using a dependency property to change the visibility of the button.
public static readonly DependencyProperty IsTeamLeaderProperty =
DependencyProperty.Register("IsTeamLeader", typeof(bool),
typeof(MainMenu), new FrameworkPropertyMetadata(false));
public bool IsTeamLeader
{
get { return (bool)GetValue(IsTeamLeaderProperty); }
set { SetValue(IsTeamLeaderProperty, value); }
}
In the “loaded event” of my user control I initialize my property with false so the button should be collapsed.
private void ViewPage_Loaded(object sender, RoutedEventArgs e)
{
this.IsTeamLeader = false;
}
But this won’t work. No matter which value the IsTeamLeader property has on startup the button is always visible.
So can you please help me and give me a hint where I did the mistake? Is there a problem with the BooleanToVisiblityConverter or is there something wrong with my dependency property implementation? Or what?
Thank you!
You must just set
DataContextlike this: