使用WPF

时间:2017-07-06 00:06:22

标签: c# wpf button mouseevent

我是WPF的新手并创建了一个带有图像的可拖动按钮 - 效果很好......但我似乎无法捕捉包含图像的按钮的onClick?

<Window x:Class="PAD.MainWindow"
    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"
    xmlns:local="clr-namespace:PAD"
    mc:Ignorable="d"
    Title="C2D" Height="134" Width="134" WindowStyle="None" Background="Transparent" Foreground="Transparent" BorderBrush="Transparent" BorderThickness="0" ResizeMode="NoResize" AllowsTransparency="True">
<Grid x:Name="ClickIcon" Background="Transparent" >

    <Button x:Name="dialButton" HorizontalAlignment="Left" VerticalAlignment="Top" Width="130" Height="130" Foreground="Transparent" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" >
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Fill="Transparent"/>
                    <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <Image x:Name="phoneIcon"  Width="128" Height="128" Source="c:\users\chapmd1\documents\visual studio 2015\Projects\PAD\PAD\red-phone-icon_300205.png" MouseDown="image_MouseDown" />
                </Grid>
            </ControlTemplate>
        </Button.Template>

    </Button>


 private void image_MouseDown(object sender, MouseButtonEventArgs e)
    {
            this.DragMove();

    }

我尝试了按钮上的onClick,但没有捕获任何内容?

2 个答案:

答案 0 :(得分:0)

您的问题可能在其他地方。这按预期工作:

<强> XAML:

<Grid x:Name="ClickIcon" Background="Transparent" >
    <Button 
        Click="dialButton_Click"
        x:Name="dialButton" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" Width="130" Height="130" 
        Foreground="Transparent" 
        Background="Transparent" 
        BorderThickness="0" 
        BorderBrush="Transparent">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid >
                    <Ellipse Fill="Transparent"/>
                    <ContentPresenter 
                        Content="{TemplateBinding Content}" 
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Center"/>
                    <Image x:Name="phoneIcon" 
                           Width="128" Height="128" 
                           Source="..."
                           MouseDown="phoneIcon_MouseDown"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>

<强> C#:

private void phoneIcon_MouseDown(object sender, MouseButtonEventArgs e)
{
    System.Diagnostics.Trace.WriteLine("phoneIcon_MouseDown");
}

private void dialButton_Click(object sender, RoutedEventArgs e)
{
    System.Diagnostics.Trace.WriteLine("dialButton_Click");
}

答案 1 :(得分:0)

不确定你想要实现什么,但是如果你想在按下按钮时拖动窗口然后再做其他事情,那么你可以这样做:

 private void image_MouseDown(object sender, MouseButtonEventArgs e)
    {
        while (e.ButtonState == MouseButtonState.Pressed)
        {
          this.DragMove();
          Debug.WriteLine("Dragged!");
        }

        var image = sender as Image;
        object parent = FindParent(image);
        while (parent != null && parent.GetType() != typeof(Button))
        {
            parent = FindParent((FrameworkElement)parent);
        }

        var bt = parent as Button;
        if(bt != null)
        bt.ClickMode = ClickMode.Press;
    }

    private void DialButton_OnClick(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Clicked!");
    }

    DependencyObject FindParent(FrameworkElement ui)
    {
        return ui.TemplatedParent;
    }
}