2009-05-20 33 views
3

你有沒有發現一個問題,當你的自定義WPF usercontrol分配一個點擊事件處理程序與嵌​​套按鈕控制?我做。爲包含按鈕的WPF用戶控件創建自定義點擊事件處理程序?

當你把這樣的用戶控件放在一個主窗口中,讓我們說Main.xaml,MouseLeftButtonDown不起作用,但是PreviewMouseLeftButtonDown就像一個魅力。

但想象一下,告訴團隊中的每個開發人員使用您的usercontrol時使用此事件...您庫中的某些usercontrols有MouseLeftButtonDown,其他人PreviewMouseLeftButtonDown ....這是一個混亂你不同意嗎?

所以我有一個解決方案,但我希望有人看看是否有一些優雅的方式來創建您的自定義事件處理程序稱爲「點擊」。

在我的用戶名爲CustomButton.xaml.cs,我到目前爲止有:

public partial class CustomButton: UserControl 
{ 

    public CustomButton() 
     : base() 
    { 
     this.InitializeComponent(); 

    } 

    public delegate void ClickHandler(object sender, EventArgs e); 
    public event EventHandler Click; 

    public void Button_Click(object sender, RoutedEventArgs e) {//execute daddy's button click 
      (((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e); 

      e.Handled = false; 

    } 

在我CustomButton.xaml

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
x:Class="YourCompany.UI.Controls.CustomButton" d:DesignHeight="72.5" d:DesignWidth="200"> 
<UserControl.Resources> 
blablabla 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot"> 
    <Button Style="{DynamicResource CustomButton}" 
      Width="{Binding ElementName=CustomButton, Path=ActualWidth}" 
      Cursor="Hand" Foreground="#ffffff" FontSize="28" Margin="8,8,0,12" 
      HorizontalAlignment="Left" 
      Content="Custom Button" Click="Button_Click" /> 


</Grid> 

現在我Main.xaml中,來電者,我有:

<Window x:Class="YourCompany.MyProject.Main" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MyProject!" Height="600" Width="800" 
MinWidth="800" MinHeight="600" WindowState="Maximized" WindowStartupLocation="CenterScreen" 
xmlns:bigbola="clr-namespace:YourCompany.UI.Controls;assembly=YourCompany.UI.Controls"> 

<mycontrols:CustomButton Name="test" MyImage=".\Images\btnOptions.png" Cursor="Hand" 
      Texto="See options" Click="test_Click" 
      Margin="168.367,176.702,253.609,0" ToolTip="See all options" Height="76.682" 
      VerticalAlignment="Top"></mycontrols:CustomButton> 

說明:

在usercontrol中,當你點擊嵌套按鈕時,它執行它的父定製「Click」處理程序。

有沒有一種優雅的方式來實現相同的效果?

回答

3

去關什麼mdm20是說......你爲什麼要創建一個用戶控件(分爲1控件的集合)時,你可以更容易地創建一個CustomControl(控制的擴展的功能現有的控制,如一個按鈕)?假設一個Button是你想在CustomButton中使用的唯一控件,我會強烈推薦你擁有的一個CustomControl(一個UserControl)。

用戶控件的

例VS CustomControl here

希望這有助於!

+1

是的,我正在閱讀您的評論,並考慮創建一個以Button爲基礎控件的自定義控件。 有很多顯示UserControl的示例,所以我開始使用Microsoft Expression Blend構建自定義按鈕。 我看到我無法控制點擊事件。但後來我將學習如何創建一個簡單而優雅的自定義按鈕。 – 2009-05-20 19:45:17

0

無法這一行:

(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e); 

通過

this.Click(sender, e); 

被取代?

除此之外,雖然答案取決於你想要的確切行爲。如果你想單擊你的用戶控件的事件,只有當你點擊內部按鈕時觸發,那麼我認爲你正在以正確的方式處理它。另一方面,如果您希望點擊事件在您點擊用戶控件範圍內的任何位置時觸發,那麼您可能是最佳樣式或繼承自標準按鈕控件。請記住,在WPF中,按鈕的內容可以是任何其他元素,包括另一個按鈕。

+0

不,它不會工作。我明白你的觀點,但我在用戶控件中有一個按鈕,這個用戶控件是一個「自定義按鈕」,它有我自己定製的自己的點擊事件。 這個想法是,當你點擊內部事件時,外部事件就會被執行! – 2009-05-20 19:47:57

1

如果你實現了一個按鈕,爲什麼不只是從按鈕派生?

要回答你的問題,所有你需要它。

if (Click != null) Click(this, EventArgs.Empty); 
+0

好的,但我想知道在哪裏附上這個聲明? – 2009-05-20 19:49:42

相關問題