| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System.Windows;
- using System.Windows.Controls;
- namespace Muchinfo.WPF.Controls.Button
- {
- /// <summary>
- /// ToggleButtonMenuControl.xaml 的交互逻辑
- /// </summary>
- public partial class ToggleButtonMenuControl : UserControl
- {
- #region 依赖属性
- public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string),
- typeof(ToggleButtonMenuControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
- new PropertyChangedCallback(TextChanged)));
- public static readonly DependencyProperty ToggleButtonTemplateProperty = DependencyProperty.Register("ToggleButtonTemplate",
- typeof(ControlTemplate), typeof(ToggleButtonMenuControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
- new PropertyChangedCallback(ToggleButtonTemplateChanged)));
- public ControlTemplate ToggleButtonTemplate
- {
- get { return (ControlTemplate)GetValue(ToggleButtonTemplateProperty); }
- set { SetValue(ToggleButtonTemplateProperty, value); }
- }
- public string Text
- {
- get { return (string)GetValue(TextProperty); }
- set { SetValue(TextProperty, value); }
- }
- private static void ToggleButtonTemplateChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
- {
- if (e.NewValue != null)
- {
- var control = (o as ToggleButtonMenuControl);
- if (control == null) return;
- //使用外部
- control.ChildToggleButton.Template = e.NewValue as ControlTemplate;
- }
- }
- private static void TextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
- {
- if (e.NewValue != null && !string.IsNullOrEmpty(e.NewValue.ToString()))
- {
- var control = (o as ToggleButtonMenuControl);
- if (control == null) return;
- control.ChildToggleButton.Content = e.NewValue;
- }
- }
- public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool?),
- typeof(ToggleButtonMenuControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
- new PropertyChangedCallback(IsCheckedChanged)));
- public bool? IsChecked
- {
- get { return (bool?)GetValue(IsCheckedProperty); }
- set { SetValue(IsCheckedProperty, value); }
- }
- private static void IsCheckedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
- {
- if (e.NewValue != null && !string.IsNullOrEmpty(e.NewValue.ToString()))
- {
- var control = (o as ToggleButtonMenuControl);
- if (control == null) return;
- control.ChildToggleButton.IsChecked = (bool?)e.NewValue;
- }
- }
- #endregion
- public ToggleButtonMenuControl()
- {
- InitializeComponent();
- }
- private void RadioBnt_OnClick(object sender, RoutedEventArgs e)
- {
- this.ChildToggleButton.IsChecked = false;
- if (this.ContextMenu != null)
- {
- if (!this.ContextMenu.IsOpen) this.ContextMenu.IsOpen = true; ;
- }
- }
- }
- }
|