6

How to assign Click event in this? I want to do something when mouse click on this window. It's doesn't have Click properties in both Window and Canvas

<Window Loaded="Window_Loaded"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="InClassApp.UI.TextNotify"
  x:Name="Window"
  Title="TextNotify"
  Width="400" Height="100"
  WindowStyle="None"
  AllowsTransparency="True"
  Background="Transparent"
  ShowInTaskbar="False">
  <Border CornerRadius="5">
    <Border.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFBAFDFF" Offset="0"/>
            <GradientStop Color="White" Offset="1"/>
        </LinearGradientBrush>
    </Border.Background>
       <Canvas x:Name="LayoutRoot" >
        .......
    </Canvas>
</Border>

Prince OfThief
  • 6,323
  • 14
  • 40
  • 53

2 Answers2

15

You could handle the MouseLeftButtonUp event instead.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
10

you might want to add MouseLeftButtonDown="Window_MouseLeftButtonDown" on your <Window> element.

and add following in the code-behind file.

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // do some stuff here.
}
Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66
  • 13
    I'd suggest handling the `MouseLeftButtonUp` event instead of `MouseLeftButtonDown`, because that's the way the `Click` event is implemented, i.e. users might be confused if the event is raised *before* they release the mouse button. – Frédéric Hamidi Nov 21 '10 at 08:08
  • I agree with Frederic here. As a user in any application, I rely on this little detail of UI processing to avoid clicking on the wrong UI element. Often enough, I click the wrong thing, and to avoid triggering a process I would rather not trigger, I keep the button held until I move the cursor away from the element. It has become second nature. I would be quite unhappy if an application triggered immediately when I pressed the button. – Daniel May 09 '22 at 18:17