如何在自定义用户控件上创建单击事件?

我已经创建了一个自定义用户控件。 我是否可以添加单击事件,以便当有人单击控件区域中的任何位置时,会触发单击事件?

用户控件定义为:

XAML:

      

C#:

 public partial class TabItem : UserControl { public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("TabItemImage", typeof(string), typeof(TabItem), null); public static readonly DependencyProperty TextProperty = DependencyProperty.Register("TabItemText", typeof(string), typeof(TabItem), null); public string TabItemImage { get { return (string)GetValue(ImageProperty); } set { SetValue(ImageProperty, value); } } public string TabItemText { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public TabItem() { InitializeComponent(); this.DataContext = this; } } 

使用简单:

  

理想情况下,我可以修改用户控件,以便我可以指定click事件,例如

   

这可能吗? 如果是这样,我需要做什么才能在自定义用户控件上创建click事件?

sthotakura,有一个好点,这将是一个很好的方式来做到这一点。 但是,如果此UserControl没有多个单击事件,为什么不使用定义自定义UserControl时带来的任何鼠标单击事件。

我不知道你可以将datacontext设置为自己…这很有趣。 我要修改我现在创建的自定义对话框.. :)感谢您教我一些东西。


XAML:

         

CS:

 public partial class TestControl : UserControl { public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("TabItemImage" , typeof(string) , typeof(TabItem) , null); public static readonly DependencyProperty TextProperty = DependencyProperty.Register("TabItemText" , typeof(string) , typeof(TabItem) , null); public string TabItemImage { get { return (string)GetValue(ImageProperty); } set { SetValue(ImageProperty , value); } } public string TabItemText { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty , value); } } public TestControl() { InitializeComponent(); this.DataContext = this; } // or private void TestControl_OnMouseDoubleClick(object sender, MouseButtonEventArgs e) { // Add logic... } // or private void TestControl_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Add logic... } // or private void TestControl_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // Add logic... } } 

您需要将自定义RoutedEvent添加到TabItem UserControl,下面是添加自定义RoutedEvent的代码:

 public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent( "Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TabItem)); public event RoutedEventHandler Click { add { AddHandler(ClickEvent, value); } remove { RemoveHandler(ClickEvent, value); } } void RaiseClickEvent() { RoutedEventArgs newEventArgs = new RoutedEventArgs(TabItem.ClickEvent); RaiseEvent(newEventArgs); } void OnClick() { RaiseClickEvent(); } 

然后在你的UserControl InitializeMethod连接PreviewMouseLeftButtonUp事件来触发你的自定义RoutedEvent:

 PreviewMouseLeftButtonUp += (sender, args) => OnClick(); 

在MSDN上讨论这个有一个非常好的操作方法,你可能想要阅读它。

所以,我可以在Sthotakura的伟大post上下水道。

这是您的所有命名空间需求的post

  using System; using System.Windows; using System.Windows.Controls; namespace YourNameSpace { partial class OPButton { ///  /// Create a custom routed event by first registering a RoutedEventID /// This event uses the bubbling routing strategy /// see the web page https://msdn.microsoft.com/EN-US/library/vstudio/ms598898(v=vs.90).aspx ///  public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(OPButton)); ///  /// Provide CLR accessors for the event Click OPButton /// Adds a routed event handler for a specified routed event Click, adding the handler to the handler collection on the current element. ///  public event RoutedEventHandler Click { add {AddHandler(ClickEvent, value); } remove { RemoveHandler(ClickEvent, value); } } ///  /// This method raises the Click event ///  private void RaiseClickEvent() { RoutedEventArgs newEventArgs = new RoutedEventArgs(OPButton.ClickEvent); RaiseEvent(newEventArgs); } ///  /// For isPressed purposes we raise the event when the OPButton is clicked ///  private void OnClick() { RaiseClickEvent(); } } } 

添加以下参考:

 Windows; PresentationCore; WindowsBase;