gpt4 book ai didi

c# - 将Button的事件订阅到自定义控件中

转载 作者:行者123 更新时间:2023-11-30 22:46:13 24 4
gpt4 key购买 nike

你知道我怎样才能订阅我的 customControl 的基础事件吗?我有一个带有一些依赖属性的自定义控件:

public class MyCustomControl : Button
{
static MyCustomControl ()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( MyCustomControl ), new FrameworkPropertyMetadata( typeof( MyCustomControl ) ) );
}

public ICommand KeyDownCommand
{
get { return (ICommand)GetValue( KeyDownCommandProperty ); }
set { SetValue( KeyDownCommandProperty, value ); }
}
public static readonly DependencyProperty KeyDownCommandProperty =
DependencyProperty.Register( "KeyDownCommand", typeof( ICommand ), typeof( MyCustomControl ) );

public ICommand KeyUpCommand
{
get { return (ICommand)GetValue( KeyUpCommandProperty ); }
set { SetValue( KeyUpCommandProperty, value ); }
}
public static readonly DependencyProperty KeyUpCommandProperty =
DependencyProperty.Register( "KeyUpCommand", typeof( ICommand ), typeof( MyCustomControl ) );

public ICommand KeyPressedCommand
{
get { return (ICommand)GetValue( KeyPressedCommandProperty ); }
set { SetValue( KeyPressedCommandProperty, value ); }
}
public static readonly DependencyProperty KeyPressedCommandProperty =
DependencyProperty.Register( "KeyPressedCommand", typeof( ICommand ), typeof( MyCustomControl ) );
}

我想订阅 Button 的事件(如 MouseLeftButtonDown)以在我的自定义控件中运行一些代码。

你知道我怎样才能在构造函数中做这样的事情吗?

static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( MyCustomControl ), new FrameworkPropertyMetadata( typeof( MyCustomControl ) ) );
MouseLeftButtonDownEvent += (object sender, MouseButtonEventArgs e) => "something";
}

谢谢你的帮助

最佳答案

我找到了解决方案!

你只需要覆盖 OnMouseLeftButtonDown方法。不要忘记在代码后调用 base.OnMouseLeftButtonDown。

关于c# - 将Button的事件订阅到自定义控件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2745089/

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