Button Visibility Converter
- by developer
Hi All,
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. Below is my code,
<StackPanel Orientation="Horizontal">
<Button Content="Create Profile" Visibility="{Binding Profile,Converter={StaticResource ButtonVisibilityConverter}, ConverterParameter='Create'}" Command="local:LaunchEditor"
CommandParameter="{Binding}"/>
<Button Content="Edit Profile" Visibility="{Binding Profile,Converter={StaticResource ButtonVisibilityConverter}, ConverterParameter='Edit'}" Command="local:LaunchEditor"
CommandParameter="{Binding}"/>
</StackPanel>
Below is my converter code
public class ButtonVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string btName = null;
btName = (string)parameter;
if ((value==null)&&(btName=="Create"))
{
return Visibility.Visible;
}
else if ((value != null) && (btName=="Edit"))
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
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. Is there any other way I could display button visibility??