クラスCustomButton
から継承した、を作成しましたButton
。そして、私が作成したいくつかのイベントなどGotFocus
、LostFocus
など
public sealed class CustomButton : Button
{
public CustomButton()
{
this.DefaultStyleKey = typeof(CustomButton);
}
protected override void OnApplyTemplate()
{
this.GotFocus += CustomButton_GotFocus;
this.LostFocus += CustomButton_LostFocus;
base.OnApplyTemplate();
}
private void CustomButton_LostFocus(object sender, RoutedEventArgs e)
{
//some common behavior code
}
private void CustomButton_GotFocus(object sender, RoutedEventArgs e)
{
//some common behavior code
}
}
それから私CustomButton
はいくつかのページでこれを使用しました。しかし、一部の特定のページでCustomButton
はGotFocus
、LostFocus
イベントを実行したくありません。
では、指定されたページでこれらのイベントをオーバーライドするにはどうすればよいですか?
指定されたページでadd GotFocus
、LostFocus
eventを試しましたが、最終的にで一般的なコードの動作が実行されCustomButton
ます。
要件については、DependencyProperty
を制御するGotFocus
か、LostFocus
イベントを実行するかどうかを指定できます。
例えば:
public class CustomButton : Button
{
public CustomButton()
{
this.DefaultStyleKey = typeof(Button);
Current = this;
}
private static CustomButton Current;
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
public bool EnableDetected
{
get { return (bool)GetValue(EnableDetectedProperty); }
set { SetValue(EnableDetectedProperty, value); }
}
// Using a DependencyProperty as the backing store for EnableDetected. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EnableDetectedProperty =
DependencyProperty.Register("EnableDetected", typeof(bool), typeof(CustomButton), new PropertyMetadata(0, new PropertyChangedCallback(OnValueChanged)));
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue == true)
{
Current.GettingFocus += Current_GettingFocus;
}
else
{
Current.GettingFocus -= Current_GettingFocus;
}
}
private static void Current_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
}
}
使用法
<local:CustomButton Content="Btn" EnableDetected="true" />
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加