首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

为什么我不能双击按钮在Silverlight 中创建事件处理程序?

在Silverlight中,双击按钮创建事件处理程序的功能并不直接支持。但是,您可以通过以下方法来实现这个功能:

  1. 在XAML代码中为按钮添加事件处理程序:
代码语言:<Button x:Name="myButton" Content="Click me" Click="myButton_Click" />
复制

然后在代码后台中定义事件处理程序:

代码语言:txt
复制
private void myButton_Click(object sender, RoutedEventArgs e)
{
    // 在这里编写您的事件处理代码
}
  1. 使用附加属性来实现双击事件处理程序:

首先,在代码后台中定义一个附加属性:

代码语言:csharp
复制
public class DoubleClickBehavior
{
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(DoubleClickBehavior), new PropertyMetadata(null, OnCommandChanged));

    public static ICommand GetCommand(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(CommandProperty);
    }

    public static void SetCommand(DependencyObject obj, ICommand value)
    {
        obj.SetValue(CommandProperty, value);
    }

    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as Control;
        if (control != null)
        {
            control.MouseLeftButtonDown += Control_MouseLeftButtonDown;
        }
    }

    private static void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var control = sender as Control;
        if (control != null && e.ClickCount == 2)
        {
            var command = GetCommand(control);
            if (command != null && command.CanExecute(null))
            {
                command.Execute(null);
            }
        }
    }
}

然后在XAML代码中为按钮添加附加属性:

代码语言:<Button x:Name="myButton" Content="Click me" local:DoubleClickBehavior.Command="{Binding MyCommand}" />
复制

最后,在代码后台中定义命令:

代码语言:csharp
复制
public ICommand MyCommand => new RelayCommand(MyCommandExecute);

private void MyCommandExecute()
{
    // 在这里编写您的事件处理代码
}

这样,您就可以在Silverlight中实现双击按钮创建事件处理程序的功能。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

6分55秒

OpenSAP Fiori Elements 公开课第四单元

14分54秒

最近我收到了 SAP 上海研究院一个部门领导的邀请,参加了一个信息素养故事分享会。我也就"如何快速上

领券