TriState Checkbox - how to change the order of the states

后端 未结 5 654
走了就别回头了
走了就别回头了 2020-12-06 17:03

I have a CheckBox in my application that is using the TriState mode. The normal behavior for this mode seems to be cycling between null, false, true.

I\'d like to c

5条回答
  •  被撕碎了的回忆
    2020-12-06 17:55

    Thank you very much, it has been very useful to solve a similar behavior with ToggleButton.

    class InvertedToggleButton : ToggleButton
    {
        public bool InvertCheckStateOrder
        {
            get { return (bool)GetValue(InvertCheckStateOrderProperty); }
            set { SetValue(InvertCheckStateOrderProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for InvertCheckStateOrder.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InvertCheckStateOrderProperty = DependencyProperty.Register("InvertCheckStateOrder", typeof(bool), typeof(ToggleButtonInvertido), new UIPropertyMetadata(false));
    
        protected override void OnToggle()
        {
            if (this.InvertCheckStateOrder)
            {
                if (this.IsChecked == true)
                {
                    this.IsChecked = false;
                }
                else if (this.IsChecked == false)
                {
                    this.IsChecked = this.IsThreeState ? null : (bool?)true;
                }
                else
                {
                    this.IsChecked = true;
                }
            }
            else
            {
                if (!this.IsChecked.HasValue)
                    this.IsChecked = true;
                else
                    base.OnToggle();
            }
        }
    }
    

提交回复
热议问题