【控件】解决WPF自定义控件鼠标左键单击无响应

【注】未亲测,如有朋友遇到问题,并解决,请不吝回复!

原文地址:

http://stackoverflow.com/questions/888647/create-a-custom-click-event-handler-for-a-wpf-usercontrol-which-contains-a-button

Hello guys

have you ever found a problem when assigning a click event handler for your custom WPF usercontrol with a nested button control? I do.

When you put such user control in a main window, let's say Main.xaml, the MouseLeftButtonDown doesn't work, but the PreviewMouseLeftButtonDown works like a charm.

But imagine yourself telling each developer in your team to use this event when using your usercontrol... Some usercontrols in you library has MouseLeftButtonDown, others PreviewMouseLeftButtonDown.... It's a mess don't you agree?

So I've got a solution but I want someone to see if there's some elegant way to create your custom event handler called "Click".

In my usercontrol called CustomButton.xaml.cs, I have so far:

 

CustomButton.xaml.cs
 
   
1 public partial class CustomButton: UserControl
2 {
3
4 public CustomButton() : base ()
5 {
6 this .InitializeComponent();
7
8 }
9
10 public delegate void ClickHandler( object sender, EventArgs e);
11 public event EventHandler Click;
12
13 public void Button_Click( object sender, RoutedEventArgs e) {
14 // execute daddy's button click
15   (((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);
16
17 e.Handled = false ;
18 }

In my CustomButton.xaml:

 

CustomButton.xaml
 
   
1 < UserControl xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
3 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
4 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
5 mc:Ignorable ="d"
6 x:Class ="YourCompany.UI.Controls.CustomButton" d:DesignHeight ="72.5" d:DesignWidth ="200" >
7   < UserControl .Resources >
8 blablabla
9   </ UserControl.Resources >
10
11   < Grid x:Name ="LayoutRoot" >
12 < Button Style ="{DynamicResource CustomButton}"
13 Width ="{Binding ElementName=CustomButton, Path=ActualWidth}"
14 Cursor ="Hand" Foreground ="#ffffff" FontSize ="28" Margin ="8,8,0,12"
15 HorizontalAlignment ="Left"
16 Content ="Custom Button" Click ="Button_Click" />
17
18   </ Grid >

Now in my Main.xaml, the caller, I have:

 

Main.xaml
 
   
1 < Window x:Class ="YourCompany.MyProject.Main"
2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title ="MyProject!" Height ="600" Width ="800"
5 MinWidth ="800" MinHeight ="600" WindowState ="Maximized" WindowStartupLocation ="CenterScreen"
6 xmlns:bigbola ="clr-namespace:YourCompany.UI.Controls;assembly=YourCompany.UI.Controls" >
7
8   < mycontrols:CustomButton Name ="test" MyImage =".\Images\btnOptions.png" Cursor ="Hand"
9 Texto ="See options" Click ="test_Click"
10 Margin ="168.367,176.702,253.609,0" ToolTip ="See all options" Height ="76.682"
11 VerticalAlignment ="Top" ></ mycontrols:CustomButton >
12

Explanation:

in the usercontrol, when you click the nested button, it executes its parent custom "Click" handler.

Is there a elegant way to accomplish the same effect?

 

 

 

 

转载于:https://www.cnblogs.com/xhzi/archive/2010/11/24/WPF.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值