gpt4 book ai didi

c# - MVVM:按钮保持事件命令

转载 作者:行者123 更新时间:2023-11-30 19:41:16 25 4
gpt4 key购买 nike

我希望能够将两个不同的 Command 分配给一个 Button:

  • 点击事件命令
  • Hold 事件命令,使用 HoldTimeout 属性指定保持时间

    public static readonly DependencyProperty HoldCommandProperty =
    DependencyProperty.Register(
    "HoldCommand",
    typeof(ICommand),
    typeof(CommandButton),
    new PropertyMetadata(null,
    CommandChanged));

    public ICommand HoldCommand
    {
    get { return (ICommand)GetValue(CommandProperty); }
    set { SetValue(CommandProperty, value); }
    }

点击&按住的时间如何计算,应该在哪里计算?如果使用按钮的“命令”属性,我不确定处理 Click 事件是否是正确的地方。

结果 XAML 应如下所示:

<CommandButton x:Name="InputButton" 
Command="{Binding PrimaryCommand}"
CommandParameter="{Binding}"
HoldCommand="{Binding SecondaryCommand}"
HoldCommandParameters="{Binding}"
HoldTimeout="2000"/>

我已经阅读了如何实现双击,但事实并非如此:

最佳答案

您需要创建一个自定义控件并使用 DispatcherTimer 类对其计时。您可以添加另一个 bool 值和命令属性来激活此行为。

控件如下:

public class SmartButton : Button
{
private DispatcherTimer _timer;


public int MillisecondsToWait
{
get { return (int)GetValue(MillisecondsToWaitProperty); }
set { SetValue(MillisecondsToWaitProperty, value); }
}

public DispatcherTimer Timer
{
get { return _timer; }
set { _timer = value; }
}



public ICommand ClickAndHoldCommand
{
get { return (ICommand)GetValue(ClickAndHoldCommandProperty); }
set { SetValue(ClickAndHoldCommandProperty, value); }
}


public bool EnableClickHold
{
get { return (bool)GetValue(EnableClickHoldProperty); }
set { SetValue(EnableClickHoldProperty, value); }
}

// 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(SmartButton), new PropertyMetadata(false));



// 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(SmartButton), new UIPropertyMetadata(null));



// 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(SmartButton), new PropertyMetadata(0));


public SmartButton()
{
this.PreviewMouseLeftButtonUp += OnPreviewMouseLeftButtonUp;
this.PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown;

}

private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (EnableClickHold)
{

bool isMouseReleaseBeforeHoldTimeout = Timer.IsEnabled;
ResetAndRemoveTimer();
// Consider it as a mouse click
if (isMouseReleaseBeforeHoldTimeout && Command != null)
{
Command.Execute(CommandParameter);
}
e.Handled = true;
}
}

private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (EnableClickHold)
{
Timer = new DispatcherTimer(DispatcherPriority.Normal, this.Dispatcher)
{
Interval = TimeSpan.FromMilliseconds(MillisecondsToWait)
};
Timer.Tick += Timer_Tick;
Timer.IsEnabled = true;
Timer.Start();
e.Handled = true;
}
}

void Timer_Tick(object sender, EventArgs e)
{
if(ClickAndHoldCommand != null)
{
this.ClickAndHoldCommand.Execute(this.CommandParameter);
}

ResetAndRemoveTimer();
}

private void ResetAndRemoveTimer()
{
if (Timer == null) return;
Timer.Tick -= Timer_Tick;
Timer.IsEnabled = false;
Timer.Stop();
Timer = null;
}
}

这个的xaml应该是这样的

 <wpfMouseClick:SmartButton x:Name="MySmartButton"
Width="100"
Height="50"
ClickAndHoldCommand="{Binding Path=MyTestCommand,
ElementName=MyWindow}"
EnableClickHold="True"
MillisecondsToWait="1000">
Click and Hold
</wpfMouseClick:SmartButton>

关于c# - MVVM:按钮保持事件命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20498177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com