【问题标题】:Capture Click event of button with image in WPF在WPF中捕获带有图像的按钮的单击事件
【发布时间】:2017-12-09 19:20:39
【问题描述】:

我是 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,但没有捕获到任何内容?

【问题讨论】:

  • 为什么要在Button.TemplateDrag 你的Window 中添加一个Image

标签: c# wpf button mouseevent


【解决方案1】:

不确定您想要实现什么,但如果您想在按下按钮时拖动窗口,然后执行其他操作,那么您可以这样做:

 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;
    }
}

【讨论】:

    【解决方案2】:

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

    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");
    }
    

    【讨论】:

    • 奇怪 - 我根本没有触发它... image_MouseDown、image_MouseDown、image_MouseDown。
    • 嗯,奇怪,真的。您是否在事件处理程序中放置断点?不用,只看 Visual Studio 中的跟踪输出。
    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2011-02-11
    相关资源
    最近更新 更多