3

ネストされたボタンコントロールを使用してカスタムWPFユーザーコントロールにクリックイベントハンドラーを割り当てるときに問題が発生したことがありますか?そうです。

このようなユーザーコントロールをメインウィンドウ(たとえばMain.xaml)に配置すると、MouseLeftButtonDownは機能しませんが、PreviewMouseLeftButtonDownは魅力のように機能します。

しかし、チーム内の各開発者に、ユーザーコントロールを使用するときにこのイベントを使用するように指示することを想像してみてください...ライブラリ内の一部のユーザーコントロールには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>

説明:

ユーザーコントロールで、ネストされたボタンをクリックすると、親のカスタム「クリック」ハンドラーが実行されます。

同じ効果を達成するためのエレガントな方法はありますか?

4

3 に答える 3

2

mdm20が言っていたことから外れて... CustomControl(ボタンなどの既存のコントロールの機能を拡張するコントロール)をはるかに簡単に作成できるのに、なぜUserControl(1にグループ化されたコントロールのコレクション)を作成するのですか)? ボタンが CustomButton で必要な唯一のコントロールであると仮定すると、持っているもの (UserControl) よりも CustomControl を強くお勧めします。

UserControl と CustomControl の例はこちら

お役に立てれば!

于 2009-05-20T15:49:40.387 に答える
1

ボタンを実装する場合、ボタンから派生させてみませんか?

あなたの質問に答えるには、これが必要です。

if (Click != null) Click(this, EventArgs.Empty);
于 2009-05-20T15:42:16.750 に答える
0

この行はできませんでした:

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

に置き換えられます

this.Click(sender, e);

?

それ以外は、答えはあなたが望む正確な動作によって異なります。ユーザーコントロールのイベントをクリックして、内側のボタンをクリックしたときにのみトリガーする場合は、正しい方法で処理していると思います。一方、ユーザー コントロールの境界内の任意の場所をクリックするたびにクリック イベントをトリガーする場合は、標準のボタン コントロールからスタイリングまたは継承するのがおそらく最適です。WPF では、ボタンのコンテンツは、別のボタンを含む他の要素にすることができることに注意してください。

于 2009-05-20T15:40:34.240 に答える