WPF:如何将单击事件分配给表单

时间:2010-11-21 06:48:02

标签: wpf canvas mouseclick-event

如何在此分配Click事件?我想在鼠标点击此窗口时执行某些操作。 它在Window和Canvas中没有Click属性

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

2 个答案:

答案 0 :(得分:12)

您可以改为处理MouseLeftButtonUp事件。

答案 1 :(得分:6)

您可能希望在MouseLeftButtonDown="Window_MouseLeftButtonDown"元素上添加<Window>

并在代码隐藏文件中添加以下内容。

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // do some stuff here.
}