当达到限制时,禁用上或下按钮的上下控件。

9 浏览
0 Comments

当达到限制时,禁用上或下按钮的上下控件。

我的问题基于对此帖子的一个答案:

Where is the WPF Numeric UpDown control?

由Squirrel.Downy先生回答。

我想要实现的是一个数字递增/递减控件,当按钮长按时,增加/减少的数量更大,否则增加/减少的数量是正常的。当达到最大/最小值时,按钮应该禁用。

我有一个基于Slider的样式,其中包含2个HoldButton类型的按钮(上/下,派生自RepeatButton)和一个只读的TextBlock用于显示值。

在HoldButton中,我有两个依赖属性用于ICommand。这些属性分别是ClickAndHoldCommand和ClickCommand,在OnPreviewMouseLeftButtonDown()或OnPreviewMouseLeftButtonUp()中执行。在xaml中,它们与Slider.IncreaseLarge和Slider.IncreaseSmall绑定。

如何在达到最大值时禁用上按钮,在达到最小值时禁用下按钮?困难在于,例如禁用滑块后,上鼠标事件不再起作用...


    
    
    
    
        
            
                
                    
                    
                        
                            
                            
                            
                            
                            
                        
                        
                        
                        
                        
                        
                        
                        
                        
                            
                        
                        
                    
                
            
        
    

public partial class HoldButton : RepeatButton
{
    private bool buttonIsHeldPressed;
    public HoldButton()
    {
        InitializeComponent();
        buttonIsHeldPressed = false;
        this.PreviewMouseLeftButtonUp += OnPreviewMouseLeftButtonUp;
        // RepeatButton fires click event repeatedly while button is being pressed!
        this.Click += HoldButton_Click;
    }
    private void HoldButton_Click(object sender, RoutedEventArgs e)
    {
        Trace.WriteLine("HoldButton_Click()");
        if (EnableClickHold)
        {
            if (numberButtonRepeats > 2)
            {
                ClickAndHoldCommand.Execute(this.CommandParameter);
                e.Handled = true;
                buttonIsHeldPressed = true;
            }
            numberButtonRepeats++;
        }
    }
    public bool EnableClickHold
    {
        get { return (bool)GetValue(EnableClickHoldProperty); }
        set { SetValue(EnableClickHoldProperty, value); }
    }
    public ICommand ClickAndHoldCommand
    {
        get { return (ICommand)GetValue(ClickAndHoldCommandProperty); }
        set { SetValue(ClickAndHoldCommandProperty, value); }
    }
    public ICommand ClickCommand
    {
        get { return (ICommand)GetValue(ClickCommandProperty); }
        set { SetValue(ClickCommandProperty, value); }
    }
    // Using a DependencyProperty as the backing store for ClickAndHoldCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ClickAndHoldCommandProperty =
        DependencyProperty.Register("ClickAndHoldCommand", typeof(ICommand), typeof(HoldButton), new UIPropertyMetadata(null));
    // Using a DependencyProperty as the backing store for ClickCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ClickCommandProperty =
        DependencyProperty.Register("ClickCommand", typeof(ICommand), typeof(HoldButton), new UIPropertyMetadata(null));
    // Using a DependencyProperty as the backing store for EnableClickHold.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnableClickHoldProperty =
        DependencyProperty.Register("EnableClickHold", typeof(bool), typeof(HoldButton), new PropertyMetadata(false));
    // Using a DependencyProperty as the backing store for MillisecondsToWait.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MillisecondsToWaitProperty =
        DependencyProperty.Register("MillisecondsToWait", typeof(int), typeof(HoldButton), new PropertyMetadata(0));
    public int MillisecondsToWait
    {
        get { return (int)GetValue(MillisecondsToWaitProperty); }
        set { SetValue(MillisecondsToWaitProperty, value); }
    }
    private int numberButtonRepeats;
    private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (EnableClickHold)
        {
            numberButtonRepeats = 0;
            if(!buttonIsHeldPressed)
            {
                ClickCommand?.Execute(this.CommandParameter);
            }
            buttonIsHeldPressed = false;
        }
    }
    private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Trace.WriteLine("OnPreviewMouseLeftButtonDown()");
        if (EnableClickHold)
        {
            // When numberButtonRepeats comes above 1 then the button is considered to be pressed long
            if (numberButtonRepeats > 1)
            {
                ClickAndHoldCommand?.Execute(this.CommandParameter);
            }
            numberButtonRepeats++;
        }
    }
}


    

0
0 Comments

问题的出现原因是在OnValueChanged()方法中,按钮对象PART_IncreaseButton和PART_DecreaseButton为空,而OnValueChanged()方法在OnApplyTemplate()方法之前执行。

解决方法是在OnValueChanged()方法中添加空引用检查,只有当按钮对象不为空时,才执行按钮的启用或禁用操作。

0