Personally, I would create a boolean property called IsButtonVisible in my view model, which implements the INotifyPropertyChanged interface.
Then I would add some kind of handler method to handle keystrokes ( KeyDown ):
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
Now the IsButtonVisible property will be updated when the key is pressed correctly, we just need to use this value to affect the Visibility Button property. To do this, we need to implement IValueConverter to convert between a boolean value and a Visibility value.
[ValueConversion(typeof(bool), typeof(Visibility))] public class BoolToVisibilityConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || value.GetType() != typeof(bool)) return null; bool boolValue = (bool)value; return boolValue ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || value.GetType() != typeof(Visibility)) return null; return (Visibility)value == Visibility.Visible; } }
Now we just need to bind to our Boolean property from the XAML Button declaration:
<Button Visibility="{Binding IsButtonVisible, Converter={StaticResource BoolToVisibilityConverter}, FallbackValue=Collapsed, Mode=OneWay}">
source share