Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Why does my WPF Control trigger twice?

$
0
0

Well as the topic states, I have a MouseDown function for my label in my WPF User Control and when I click the label in my application, the event is triggered twice.

Here is the code for the actual WPF Control.

WPF:

<UserControl x:Class="NexusLog.UI_Elements.nexusButton"
             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" 
             d:DesignHeight="126" d:DesignWidth="389" Loaded="UserControl_Loaded" MouseDown="mouseDown" MouseEnter="mouseEnter" MouseLeave="mouseLeave" MouseUp="mouseUp"><Grid><Label Content="Label" Margin="10,12,12,10" Name="lblText" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontFamily="BigNoodleTitling" FontSize="72" MouseDown="lblText_MouseDown" MouseEnter="mouseEnter" MouseLeave="mouseLeave" MouseUp="mouseUp" /><Rectangle Margin="0,120,0,0" Name="rectUnderline" Stroke="#00000000" Fill="Black" MouseDown="mouseDown" MouseEnter="mouseEnter" MouseLeave="mouseLeave" MouseUp="mouseUp"/></Grid></UserControl>


C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 NexusLog.UI_Elements
{
    /// <summary>
    /// Interaction logic for nexusButton.xaml
    /// </summary>
    public partial class nexusButton : UserControl
    {
        Brush controlForegroundActive = new SolidColorBrush(Color.FromRgb(248, 134, 19));
        Brush controlForegroundInactive = new SolidColorBrush(Color.FromRgb(209, 209, 209));

        public event MouseEventHandler OnClick;

        public nexusButton()
        {
            InitializeComponent();
        }

        public string NexusText
        {
            get { return lblText.Content.ToString(); }
            set { lblText.Content = value; }
        }

        public double NexusUnderlineSize
        {
            get { return rectUnderline.Height; }
            set { rectUnderline.Height = value; }
        }

        public double NexusFontSize
        {
            get { return lblText.FontSize; }
            set { lblText.FontSize = value; }
        }

        public Brush NexusActiveColor
        {
            get { return controlForegroundActive; }
            set { controlForegroundActive = value; }
        }

        public Brush NexusInactiveColor
        {
            get { return controlForegroundInactive; }
            set { controlForegroundInactive = value; }
        }

        private void mouseUp(object sender, MouseButtonEventArgs e)
        {
            lblText.Foreground = controlForegroundInactive;
            rectUnderline.Fill = controlForegroundInactive;            
        }
        private void mouseDown(object sender, MouseButtonEventArgs e)
        {
            lblText.Foreground = controlForegroundActive;
            rectUnderline.Fill = controlForegroundActive;
        }
        private void mouseEnter(object sender, MouseEventArgs e)
        {
            lblText.Foreground = controlForegroundActive;
            rectUnderline.Fill = controlForegroundActive;
        }
        private void mouseLeave(object sender, MouseEventArgs e)
        {
            lblText.Foreground = controlForegroundInactive;
            rectUnderline.Fill = controlForegroundInactive;
        }

        private void lblText_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mouseDown(sender, e);
            OnClick.Invoke(sender, e);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            lblText.Foreground = controlForegroundInactive;
            rectUnderline.Fill = controlForegroundInactive;
        }
    }
}


In My Application:

private void nexusButton1_OnClick(object sender, MouseEventArgs e)
{
      MessageBox.Show("TEST");
}

Any help is greatly appreciated. Thanks!




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>