| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using Microsoft.Xaml.Behaviors;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- namespace TeamAAS_VP.Behaviors
- {
- public class RadioButtonUserSelectionBehavior : Behavior<RadioButton>
- {
- public static readonly DependencyProperty UserSelectionCommandProperty =
- DependencyProperty.Register(
- nameof(UserSelectionCommand),
- typeof(ICommand),
- typeof(RadioButtonUserSelectionBehavior));
- public ICommand UserSelectionCommand
- {
- get => (ICommand)GetValue(UserSelectionCommandProperty);
- set => SetValue(UserSelectionCommandProperty, value);
- }
- public static readonly DependencyProperty CommandParameterProperty =
- DependencyProperty.Register(
- nameof(CommandParameter),
- typeof(object),
- typeof(RadioButtonUserSelectionBehavior));
- public object CommandParameter
- {
- get => GetValue(CommandParameterProperty);
- set => SetValue(CommandParameterProperty, value);
- }
- private bool _isUserInteraction = false;
- protected override void OnAttached()
- {
- base.OnAttached();
- AssociatedObject.PreviewMouseDown += OnPreviewMouseDown;
- AssociatedObject.PreviewKeyDown += OnPreviewKeyDown;
- AssociatedObject.Checked += OnChecked;
- }
- protected override void OnDetaching()
- {
- base.OnDetaching();
- AssociatedObject.PreviewMouseDown -= OnPreviewMouseDown;
- AssociatedObject.PreviewKeyDown -= OnPreviewKeyDown;
- AssociatedObject.Checked -= OnChecked;
- }
- private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
- {
- _isUserInteraction = true;
- }
- private void OnPreviewKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Space)
- {
- _isUserInteraction = true;
- }
- }
- private void OnChecked(object sender, RoutedEventArgs e)
- {
- if (_isUserInteraction && UserSelectionCommand?.CanExecute(CommandParameter) == true)
- {
- var parameter = new Tuple<object, object>(CommandParameter, AssociatedObject.Content);
- UserSelectionCommand.Execute(parameter);
- }
- _isUserInteraction = false;
- }
- }
- }
|