Button Visibility Converter
I have a requirement wherein I need to display on a User form, a Create Button if that user has doesnt have a profile and a Edit Button if he does have a profile. I am using a converter to change the visibility of the button. Everything works fine initially, but the moment I click on the Create Profile window, and it is the constructor that loads the profile window, after that even if I close that window without doing anything, the Create button turns to edit. My guess is that, it is because the constructor would have create the profile object and so even though the object is empty it shows me edit button instead of create开发者_如何学Go. Is there any other way I could display button visibility??
Do you really need to alter the button's visibility? This is not generally a great user-experience practice as it can confuse people about what they need to do next.
To simply disable the button, you could add logic to the local:LaunchEditor
command's CanExecute
method. There's also a CanExecuteChanged
event which allows the UI to dynamically respond to changes in the command's accessibility. This may be the easiest thing to do.
If you really need to hide the button, then you can probably create a style with a trigger based on the button's IsEnabled
property which will make the button's visibility Hidden
when IsEnabled==false
. You should, however, still use the command to control the accessibility of the button.
In your case, you would write two methods: CanCreateProfile
and CanEditProfile
, and include these in your command binding (either in XAML or in the codebehind):
private void CanCreateProfile(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = userProfile == null;
}
private void CanEditProfile(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = userProfiel != null;
}
精彩评论