您知道如何訂閱我的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));
}
我whant訂閱按鈕的事件(如的MouseLeftButtonDown)在我customControl運行一些代碼。
你知道我該如何在構造函數中做這樣的事嗎?
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
MouseLeftButtonDownEvent += (object sender, MouseButtonEventArgs e) => "something";
}
感謝您幫助
謝謝克裏克:) – 2010-05-01 12:37:26