Czemu nie działa mi event MouseLeftButtonDown w WPF

0

Mam następującą userControlkę :

<UserControl x:Class="MP3PlayerProject.StandardControl.TransparentButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             Name="UC1">
        <Style x:Key="MyStyle" TargetType="Button">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Button  Style="{StaticResource MyStyle}" >
        <Image RenderOptions.BitmapScalingMode="HighQuality" Margin="0" Source="{Binding ElementName=UC1, Path=ImageSourceMouseOut}" Name="Img1" MouseEnter="Img1_MouseEnter" MouseLeave="Img1_MouseLeave" />

    </Button>
</UserControl>
 

Kod klasy:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MP3PlayerProject.StandardControl
{
    public partial class TransparentButton : UserControl
    {
        //Nasza właściwość która zawiera adres do obrazka gdy nie jest nad nim mysz
        public static readonly DependencyProperty ImageSourceMouseOutProperty =
     DependencyProperty.Register("ImageSourceMouseOut", typeof(ImageSource),
     typeof(TransparentButton), new FrameworkPropertyMetadata(null));

        public ImageSource ImageSourceMouseOut
        {
            get { return GetValue(ImageSourceMouseOutProperty) as ImageSource; }
            set { SetValue(ImageSourceMouseOutProperty, value); SetValue(ImageSourceCurrentProperty, value);
                Img1.Source = ImageSourceCurrent;
            }
        }
        //Nasza właściwość która zawiera adres do obrazka gdy jest nad nim mysz
        public static readonly DependencyProperty ImageSourceMouseInProperty =
     DependencyProperty.Register("ImageSourceMouseIn", typeof(ImageSource),
     typeof(TransparentButton), new FrameworkPropertyMetadata(null));

        public ImageSource ImageSourceMouseIn
        {
            get { return GetValue(ImageSourceMouseInProperty) as ImageSource; }
            set { SetValue(ImageSourceMouseInProperty, value); }
        }
        //Nasza właściwość która zawiera aktualnie wyświetlany obrazek
        public static readonly DependencyProperty ImageSourceCurrentProperty =
     DependencyProperty.Register("ImageSourceCurrent", typeof(ImageSource),
     typeof(TransparentButton), new FrameworkPropertyMetadata(null));

        public ImageSource ImageSourceCurrent
        {
            get { return GetValue(ImageSourceCurrentProperty) as ImageSource; }
            set { SetValue(ImageSourceCurrentProperty, value); }
        }

        public TransparentButton()
        {
            InitializeComponent();
        }

        private void Img1_MouseEnter(object sender, MouseEventArgs e)
        {
            ImageSourceCurrent = ImageSourceMouseIn;
            Img1.Source = ImageSourceCurrent;
        }

        private void Img1_MouseLeave(object sender, MouseEventArgs e)
        {
            ImageSourceCurrent = ImageSourceMouseOut;
            Img1.Source = ImageSourceCurrent;
        }
    }
}
 

I teraz gdy chcę skorzystać z kontrolki i próbuje zaprogramować ją pod przycisk down left myszki nic się nie dzieje gdy klikam lewą myszką, sprawdziłem breakpointem, co dziwne wydarzenie doubleClick działa. Tutaj kod jak to wywołuje:

        <standardControl:TransparentButton x:Name="star2"  HorizontalAlignment="Center" ImageSourceMouseIn="Assets/ControlImages/Stars/Star_Full.png" ImageSourceMouseOut="Assets/ControlImages/Stars/Star_Empty.png" Margin="5"  MouseDoubleClick="star2_MouseDoubleClick" MouseLeftButtonDown="star2_MouseLeftButtonDown"/>
 

Czemu tak się dzieje ?

0

Nie możesz użyć w klasie Button zdarzenia MouseLeftButtonDown/MouseLeftButtonUp ponieważ jest używane do zdarzenia Click. Więc pytanie czemu nie użyjesz po prostu Click? Jako obejście możesz użyć:

  1. AddHandler
  2. PreviewMouseDown event
    Więcej na temat: https://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttondown.aspx
0

Click nie jest dostępny, poradziłem sobie używając zamiast buttona labela

1 użytkowników online, w tym zalogowanych: 0, gości: 1