【控件】解决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?

 

 

 

 

posted @ 2010-11-24 19:11  玄黄道长  阅读(2996)  评论(2编辑  收藏  举报