C# 订阅按钮';将事件添加到自定义控件中
您知道如何订阅customControl基础的事件吗? 我有一个自定义控件,具有一些依赖属性:C# 订阅按钮';将事件添加到自定义控件中,c#,wpf,events,custom-controls,C#,Wpf,Events,Custom Controls,您知道如何订阅customControl基础的事件吗? 我有一个自定义控件,具有一些依赖属性: public class MyCustomControl : Button { static MyCustomControl () { DefaultStyleKeyProperty.OverrideMetadata( typeof( MyCustomControl ), new FrameworkPropertyMetadata( typeof( MyCustomCo
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)来运行customControl中的一些代码
你知道如何在构造函数中执行类似的操作吗
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( MyCustomControl ), new FrameworkPropertyMetadata( typeof( MyCustomControl ) ) );
MouseLeftButtonDownEvent += (object sender, MouseButtonEventArgs e) => "something";
}
谢谢你的帮助我找到了解决方案
您只需重写方法。不要忘记在您的代码之后调用base.OnMouseLeftButtonDown。好的,这可能会起作用。如果您希望xaml文件中没有任何代码,并且希望遵守MVVM,那么我建议您研究附加的行为 以下是一个链接: 这是一个很好的资源,就在上周救了我