У меня есть следующий класс/поведение, которое вызывается при событии нажатия кнопки. Проблема в том, что он вызывается после события button_click. Как вызвать его перед событием нажатия кнопки? нижеуказанному стиль определяется в App.xaml для глобального использованияПриведенное поведение WPF после нажатия кнопки
XAML:
<Style TargetType="Button">
<Setter Property="local:DefaultButtonBehaviour.DefaultButton" Value="True" />
</Style>
КОД:
public static class DefaultButtonBehaviour
{
/// 1. This is the boolean attached property with its getter and setter:
public static readonly DependencyProperty DefaultButtonProperty =
DependencyProperty.RegisterAttached
(
"DefaultButton",
typeof(bool),
typeof(DefaultButtonBehaviour),
new UIPropertyMetadata(false, OnDefaultButtonPropertyChanged)
);
public static bool GetDefaultButton(DependencyObject obj)
{
return (bool)obj.GetValue(DefaultButtonProperty);
}
private static void SetDefaultButton(DependencyObject obj, bool value)
{
obj.SetValue(DefaultButtonProperty, value);
}
/// 2. This is the change event of our attached property value:
/// * We get in the first parameter the dependency object to which the attached behavior was attached
/// * We get in the second parameter the value of the attached behavior.
/// * The implementation of the behavior is to check if we are attached to a textBox, and if so and the value of the behavior
/// is true, hook to the PreviewGotKeyboardFocus of the textbox.
private static void OnDefaultButtonPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
{
ButtonBase button = dpo as ButtonBase;
if (button != null)
{
if ((bool)args.NewValue)
{
button.Click += OnDefaultButtonClick;
}
else
{
button.Click -= OnDefaultButtonClick; ;
}
}
}
private static void OnDefaultButtonClick(object sender, RoutedEventArgs e)
{
ButtonBase btn = (ButtonBase)sender;
DependencyObject focusScope = FocusManager.GetFocusScope(btn);
FocusManager.SetFocusedElement(focusScope, btn);
Keyboard.Focus(btn);
}
}
Как вы дифференцируете «до» и «после» щелчка? Событие Click - это событие, и все обработчики вызываются после того, как событие уже произошло, вам понадобится хрустальный шар, чтобы вызвать их до возникновения события. Возможно, я не получу ваш вопрос. –
Спасибо, что посмотрели. У меня есть событие нажатия кнопки на одном из экранов. Сначала нажатие кнопки запускается, а затем вызывает это поведение. Я хочу, чтобы все было наоборот. – Alice
Извините, я все еще не слежу, давайте скажем, что вы не прикрепляете какое-либо поведение к клику, что происходит при щелчке в вашем случае? –