using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Muchinfo.DataPager.Base { public class MuchinfoRoutedEventArgs : RoutedEventArgs { public MuchinfoRoutedEventArgs() { } public MuchinfoRoutedEventArgs(RoutedEvent routedEvent) : this(routedEvent, null) { } public MuchinfoRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { } } public class MuchinfoPagerNumberRadioButton : RadioButton { public static readonly DependencyProperty CornerRadiusProperty; [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public static readonly DependencyProperty InnerCornerRadiusProperty; public static readonly DependencyProperty IsBackgroundVisibleProperty; public static readonly RoutedEvent ActivateEvent; public event EventHandler Activate { add { base.AddHandler(MuchinfoPagerNumberRadioButton.ActivateEvent, value); } remove { base.RemoveHandler(MuchinfoPagerNumberRadioButton.ActivateEvent, value); } } public CornerRadius CornerRadius { get { return (CornerRadius)base.GetValue(MuchinfoPagerNumberRadioButton.CornerRadiusProperty); } set { base.SetValue(MuchinfoPagerNumberRadioButton.CornerRadiusProperty, value); } } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public CornerRadius InnerCornerRadius { get { return (CornerRadius)base.GetValue(MuchinfoPagerNumberRadioButton.InnerCornerRadiusProperty); } set { base.SetValue(MuchinfoPagerNumberRadioButton.InnerCornerRadiusProperty, value); } } [Browsable(false)] public bool IsBackgroundVisible { get { return (bool)base.GetValue(MuchinfoPagerNumberRadioButton.IsBackgroundVisibleProperty); } set { base.SetValue(MuchinfoPagerNumberRadioButton.IsBackgroundVisibleProperty, value); } } static MuchinfoPagerNumberRadioButton() { MuchinfoPagerNumberRadioButton.CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MuchinfoPagerNumberRadioButton), new PropertyMetadata(new PropertyChangedCallback(MuchinfoPagerNumberRadioButton.OnCornerRadiusChanged))); MuchinfoPagerNumberRadioButton.InnerCornerRadiusProperty = DependencyProperty.Register("InnerCornerRadius", typeof(CornerRadius), typeof(MuchinfoPagerNumberRadioButton), new PropertyMetadata()); MuchinfoPagerNumberRadioButton.IsBackgroundVisibleProperty = DependencyProperty.Register("IsBackgroundVisible", typeof(bool), typeof(MuchinfoPagerNumberRadioButton), new PropertyMetadata(true, new PropertyChangedCallback(MuchinfoPagerNumberRadioButton.OnIsBackgroundVisiblePropertyChanged))); MuchinfoPagerNumberRadioButton.ActivateEvent = EventManager.RegisterRoutedEvent("Activate", RoutingStrategy.Bubble, typeof(EventHandler), typeof(MuchinfoPagerNumberRadioButton)); FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MuchinfoPagerNumberRadioButton), new FrameworkPropertyMetadata(typeof(MuchinfoPagerNumberRadioButton))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); this.UpdateVisualStates(); this.UpdateBackgroundVisibility(); } internal void UpdateVisualStates() { if (!base.IsEnabled) { if (base.IsChecked == true) { VisualStateManager.GoToState(this, "DisabledChecked", true); } else { VisualStateManager.GoToState(this, "Disabled", true); } } else { if (base.IsPressed) { VisualStateManager.GoToState(this, "Pressed", true); } else { if (base.IsMouseOver) { if (base.IsChecked == true) { VisualStateManager.GoToState(this, "MouseOverChecked", true); } else { VisualStateManager.GoToState(this, "MouseOver", true); } } else { VisualStateManager.GoToState(this, "Normal", true); } } } if (base.IsChecked == true) { VisualStateManager.GoToState(this, "Checked", true); } else { if (base.IsChecked == false) { VisualStateManager.GoToState(this, "Unchecked", true); } else { if (!VisualStateManager.GoToState(this, "Indeterminate", true)) { VisualStateManager.GoToState(this, "Unchecked", true); } } } if (base.IsFocused && base.IsEnabled) { VisualStateManager.GoToState(this, "Focused", true); } else { VisualStateManager.GoToState(this, "Unfocused", true); } if (this.IsBackgroundVisible || base.IsMouseOver || base.IsPressed) { VisualStateManager.GoToState(this, "BackgroundVisible", true); } else { VisualStateManager.GoToState(this, "BackgroundHidden", true); } if (this.IsBackgroundVisible) { VisualStateManager.GoToState(this, "BackgroundIsVisible", false); return; } VisualStateManager.GoToState(this, "BackgroundIsHidden", false); } internal void UpdateBackgroundVisibility() { this.UpdateVisualStates(); } internal void OnToggleInternal() { this.OnToggle(); } protected internal virtual void OnActivate() { base.RaiseEvent(new MuchinfoRoutedEventArgs(MuchinfoPagerNumberRadioButton.ActivateEvent, this)); } protected override void OnToggle() { base.OnToggle(); this.OnActivate(); //if (base.IsChecked == true) //{ // TraceMonitor.TrackAtomicFeature(this, "Checked"); // return; //} //if (base.IsChecked == false) //{ // TraceMonitor.TrackAtomicFeature(this, "Unchecked"); //} } protected override void OnMouseEnter(MouseEventArgs e) { base.OnMouseEnter(e); this.UpdateVisualStates(); this.UpdateBackgroundVisibility(); } protected override void OnMouseLeave(MouseEventArgs e) { base.OnMouseLeave(e); this.UpdateVisualStates(); this.UpdateBackgroundVisibility(); } protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); this.UpdateVisualStates(); this.UpdateBackgroundVisibility(); } protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); this.UpdateBackgroundVisibility(); } protected override void OnGotFocus(RoutedEventArgs e) { base.OnGotFocus(e); this.UpdateBackgroundVisibility(); } private static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MuchinfoPagerNumberRadioButton radRadioButton = d as MuchinfoPagerNumberRadioButton; CornerRadius cornerRadius = (CornerRadius)e.NewValue; if (radRadioButton != null) { radRadioButton.InnerCornerRadius = new CornerRadius(Math.Max(0.0, cornerRadius.TopLeft - 1.0), Math.Max(0.0, cornerRadius.TopRight - 1.0), Math.Max(0.0, cornerRadius.BottomRight - 1.0), Math.Max(0.0, cornerRadius.BottomLeft - 1.0)); } } private static void OnIsBackgroundVisiblePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MuchinfoPagerNumberRadioButton radRadioButton = d as MuchinfoPagerNumberRadioButton; } } }