2016-11-24 163 views
3

我有問題,我有一個內部的Stackpanel,這是一個ScrollViewer內的菜單。現在即使水平滾動條被隱藏,也可以水平滾動。現在的主要問題是,我無法爲按鈕製作Clickevent。看來我的Scroll Event阻止了它下面的東西。WPF點擊按鈕滾動查看器

enter image description here

的XAML代碼

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="200"/> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="200"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="110"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="100"/> 
    </Grid.RowDefinitions> 
    <Rectangle x:Name="rectangel1" Grid.RowSpan="3"> 
     <Rectangle.Fill> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="#FF0036A0" Offset="0.003"/> 
       <GradientStop Color="#FFE9EDFF" Offset="1"/> 
      </LinearGradientBrush> 
     </Rectangle.Fill> 
    </Rectangle> 
    <Grid Grid.Column="1" Grid.ColumnSpan="2"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="20"/> 
      <ColumnDefinition Width="750"/> 
      <ColumnDefinition Width="20"/> 
     </Grid.ColumnDefinitions> 
     <ScrollViewer x:Name="scrollviewer1" Grid.Column="1" Grid.ColumnSpan="2" 
      HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled" 
      PanningMode="HorizontalOnly" 
      MouseUp="UIElement_OnMouseUp" 
      PreviewMouseLeftButtonDown="UIElement_OnPreviewMouseLeftButtonDown" 
      PreviewMouseMove="UIElement_OnPreviewMouseMove" Cursor="Hand"> 

      <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.ColumnSpan="1"> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn1" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="No Btn 1" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn2" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="No Btn 2" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <!--<Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn3" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 3" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid>--> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Button x:Name="btn3" Content="Station 3" Click="btn3_Click" Style="{DynamicResource ButtonStyle1}" > 
         <Button.Resources> 
          <Style x:Key="ButtonFocusVisual"> 
           <Setter Property="Control.Template"> 
            <Setter.Value> 
             <ControlTemplate> 
              <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
          <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"> 
           <GradientStop Color="#F3F3F3" Offset="0"/> 
           <GradientStop Color="#EBEBEB" Offset="0.5"/> 
           <GradientStop Color="#DDDDDD" Offset="0.5"/> 
           <GradientStop Color="#CDCDCD" Offset="1"/> 
          </LinearGradientBrush> 
          <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> 
          <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
           <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
           <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
           <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
           <Setter Property="BorderThickness" Value="1"/> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
           <Setter Property="HorizontalContentAlignment" Value="Center"/> 
           <Setter Property="VerticalContentAlignment" Value="Center"/> 
           <Setter Property="Padding" Value="1"/> 
           <Setter Property="Template"> 
            <Setter.Value> 
             <ControlTemplate TargetType="{x:Type Button}"> 
              <Grid> 
               <Rectangle Fill="#FFFFA6A6" RadiusY="10" RadiusX="10"/> 
               <Label x:Name="label" Content="BUTTON" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
              </Grid> 
              <ControlTemplate.Triggers> 
               <Trigger Property="IsKeyboardFocused" Value="true"/> 
               <Trigger Property="ToggleButton.IsChecked" Value="true"/> 
               <Trigger Property="IsEnabled" Value="false"> 
                <Setter Property="Foreground" Value="#ADADAD"/> 
               </Trigger> 
              </ControlTemplate.Triggers> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </Button.Resources> 
        </Button> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn4" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="No Btn 4" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn5" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="No Btn 5" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn6" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="No Btn 6" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn7" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="No Btn 7" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle x:Name="btn8" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="No Btn 8" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,50,10"> 
        <Button x:Name="btn9" Content="Station 9" Click="btn9_Click" Style="{DynamicResource ButtonStyle1}" > 
         <Button.Resources> 
          <Style x:Key="ButtonFocusVisual"> 
           <Setter Property="Control.Template"> 
            <Setter.Value> 
             <ControlTemplate> 
              <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
          <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"> 
           <GradientStop Color="#F3F3F3" Offset="0"/> 
           <GradientStop Color="#EBEBEB" Offset="0.5"/> 
           <GradientStop Color="#DDDDDD" Offset="0.5"/> 
           <GradientStop Color="#CDCDCD" Offset="1"/> 
          </LinearGradientBrush> 
          <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> 
          <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
           <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
           <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
           <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
           <Setter Property="BorderThickness" Value="1"/> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
           <Setter Property="HorizontalContentAlignment" Value="Center"/> 
           <Setter Property="VerticalContentAlignment" Value="Center"/> 
           <Setter Property="Padding" Value="1"/> 
           <Setter Property="Template"> 
            <Setter.Value> 
             <ControlTemplate TargetType="{x:Type Button}"> 
              <Grid> 
               <Rectangle Fill="#FFFFA6A6" RadiusY="10" RadiusX="10"/> 
               <Label x:Name="label" Content="Button 9" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
              </Grid> 
              <ControlTemplate.Triggers> 
               <Trigger Property="IsKeyboardFocused" Value="true"/> 
               <Trigger Property="ToggleButton.IsChecked" Value="true"/> 
               <Trigger Property="IsEnabled" Value="false"> 
                <Setter Property="Foreground" Value="#ADADAD"/> 
               </Trigger> 
              </ControlTemplate.Triggers> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </Button.Resources> 
        </Button> 
       </Grid> 
      </StackPanel> 

     </ScrollViewer> 

     <Rectangle x:Name="rectlinks" Grid.Column="0" Fill="#FFFF7676" MouseEnter="rectlinks_MouseEnter" /> 
     <Rectangle x:Name="rectrechts" Grid.Column="2" MouseEnter="rectrechts_MouseEnter" Fill="#FFFF7474" /> 
    </Grid> 
    <Label x:Name="label1" Content="Wert"/> 
    <Label x:Name="label2" Content="mehr" Margin="0,50,0,0"/> 


</Grid> 

C#代碼

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 ScrollMenue 
{ 
    /// <summary> 
    /// Interaktionslogik für MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private double hOff; 

     public MainWindow() 
     { 
      InitializeComponent(); 



     } 



     private void rectrechts_MouseEnter(object sender, MouseEventArgs e) 
     { 
      label1.Content = "rechts"; 
      scrollviewer1.LineRight(); 
     } 

     private void rectlinks_MouseEnter(object sender, MouseEventArgs e) 
     { 

      if (rectlinks.IsMouseOver == true) 
      { 
       scrollviewer1.ScrollToHorizontalOffset(scrollviewer1.HorizontalOffset + 10); 
       label1.Content = "links"; 
       scrollviewer1.LineLeft(); 
      } 
     } 


     private Point scrollMousePoint; 

     private void UIElement_OnMouseUp(object sender, MouseButtonEventArgs e) 
     { 
      scrollviewer1.ReleaseMouseCapture(); 
     } 

     private void UIElement_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      scrollviewer1.CaptureMouse(); 
      scrollMousePoint = e.GetPosition(scrollviewer1); 
      hOff = scrollviewer1.HorizontalOffset; 
     } 

     private void UIElement_OnPreviewMouseMove(object sender, MouseEventArgs e) 
     { 
      if (scrollviewer1.IsMouseCaptured) 
      { 
       scrollviewer1.ScrollToHorizontalOffset(hOff + (scrollMousePoint.X - e.GetPosition(scrollviewer1).X)); 
      } 
     } 

     private void btn9_Click(object sender, RoutedEventArgs e) 
     { 
      label2.Content = "Click"; 
     } 

     private void btn3_Click(object sender, RoutedEventArgs e) 
     { 
      label2.Content = "Click"; 
     } 
    } 
} 
+0

您的Xaml代碼是一團糟,您可以通過刪除與您的特定問題無關的東西來簡化它嗎?順便說一句,是什麼讓你覺得ScrollViewer與點擊事件有關? – 3615

+0

@ 3615,他的滾動查看與點擊事件相關,因爲他通過按下鼠標按鈕來滾動。我喜歡他擁有所有代碼,因爲我可以將它粘貼到我的VS中並運行它。 – JohnChris

+1

@JohnChris我同意這個問題必須是完整且可驗證的,但它也必須是** Minimal **,如[如何創建一個最小,完整和可驗證的示例](http:// stackoverflow。com/help/mcve) – 3615

回答

0

我發現你的解決方案,

我只是做了一個按鈕,但你可以做所有,

private void UIElement_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     if (e.Source == btn3) 
     { btn3_Click(sender, e); } 

     scrollMousePoint = e.GetPosition(scrollviewer1); 
     hOff = scrollviewer1.HorizontalOffset; 
     scrollviewer1.CaptureMouse(); 
    } 

    private void btn9_Click(object sender, RoutedEventArgs e) 
    { 
     label2.Content = "i AM WORKING"; //You can press and still scroll 
    } 

UPDATE:

從你說你現在想感動,你可能只是想滾動時不會觸發按鈕,你需要實現某種類型的邏輯,如停留手勢的意見,這裏是對

Do WPF have Touch-and-Hold gesture?

更新後2:

雅Ÿ我做到了,這是一個非常簡單的解決方案,

添加另一個處理程序中的XAML的PreviewMouseLeftButtonUp然後在後面的代碼(C#)做到這一點:

 private DateTime mouseTimer; 
    private void UIElement_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     mouseTimer = DateTime.Now; 
     scrollviewer1.CaptureMouse(); 
     scrollMousePoint = e.GetPosition(scrollviewer1); 
     hOff = scrollviewer1.HorizontalOffset; 

    } 

    private void scrollviewer1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     TimeSpan difference = DateTime.Now - mouseTimer; 

     if (difference.TotalSeconds < 1) 
     { 
      btn3_Click(sender, e); 
     } 
     else 
      return; 

    } 

所以,現在,如果它的點擊,它打開,但你持有的點擊,它只會滾動,日期時間的使用是真棒有

+0

嗨JohnChris,你的例子很好,但它並沒有解決問題,因爲當你開始滾動滾動查看器,它需要點擊事件,如果你在按鈕上,你不能再滾動:D 我認爲還有一點點要做。 – PCode

+0

@PCode,刪除消息框,並將標籤更新和它的作品,你可以點擊按鈕,它會更新標籤的內容,你仍然可以滾動。我更新了我的答案以顯示標籤,我已嘗試過,它已更新,並且您可以單擊按鈕並滾動 – JohnChris

+0

您的示例工程,但首先應該採用拖動(滾動)事件.... 在您的示例中,點擊仍然滾動什麼是好的,但不是我在找什麼,因爲當我的Button事件會打開其他視圖或用戶控件它會打開它,但我不能滾動菜單中的(例如)23按鈕: D 你知道我的意思嗎? 如果我想回滾到按鈕23(例如),我會開始滾動按鈕3上的鼠標按鈕它將首先調用按鈕3事件,然後滾動,但當按鈕3切換窗口之間時,我會離開Scrollview – PCode

1

所以,現在我發現了我想要的解決方案,從JohnChris幫助和其他優秀的程序員:d感謝所有:d

必須添加對項目的引用,UIAutomationProvi der.dll 和兩個using語句,使用System.Windows.Automation.Peers和System.Windows.Automation.Provider,這些允許和調用提供的使用同行

的XAML代碼:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="200"/> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="200"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="110"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="100"/> 
    </Grid.RowDefinitions> 
    <Rectangle Name="rectangel1" Grid.RowSpan="3"> 
     <Rectangle.Fill> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="#FF0036A0" Offset="0.003"/> 
       <GradientStop Color="#FFE9EDFF" Offset="1"/> 
      </LinearGradientBrush> 
     </Rectangle.Fill> 
    </Rectangle> 
    <Grid Grid.Column="1" Grid.ColumnSpan="2"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="20"/> 
      <ColumnDefinition Width="750"/> 
      <ColumnDefinition Width="20"/> 
     </Grid.ColumnDefinitions> 
     <ScrollViewer Name="scrollviewer1" Grid.Column="1" Grid.ColumnSpan="2" 
         HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled" 
         PanningMode="HorizontalOnly" 
         MouseUp="UIElement_OnMouseUp" 
         PreviewMouseLeftButtonDown="UIElement_OnPreviewMouseLeftButtonDown" 
         PreviewMouseMove="UIElement_OnPreviewMouseMove" 
         > 

      <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.ColumnSpan="1"> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle Name="btn1" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 1" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle Name="btn2" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 2" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle Name="btn3" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <!--<Label Content="Station 3" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/>--> 
        <Button Name="BtnFoo" Click="ButtonBase_OnClick">Foo!</Button> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle Name="btn4" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 4" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle Name="btn5" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 5" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle Name="btn6" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 6" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,10,10"> 
        <Rectangle Name="btn7" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 7" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
       <Grid Width="87" Height="90" Margin="10,10,50,10"> 
        <Rectangle Name="btn8" Fill="#FFF39999" RadiusX="10" RadiusY="10" /> 
        <Label Content="Station 8" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/> 
       </Grid> 
      </StackPanel> 

    </ScrollViewer> 

    <Rectangle Name="rectlinks" Grid.Column="0" Fill="#FFFF7676" MouseEnter="rectlinks_MouseEnter" /> 
    <Rectangle Name="rectrechts" Grid.Column="2" MouseEnter="rectrechts_MouseEnter" Fill="#FFFF7474" /> 
</Grid> 
<Label Name="label1" Content="Wert"/> 
    <Label Name="label2" Content="mehr" Margin="0,50,0,0"/> 


</Grid> 

的C#代碼:

namespace ScrollMenue 
{ 
    /// <summary> 
    /// Interaktionslogik für MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private double hOff; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void rectrechts_MouseEnter(object sender, MouseEventArgs e) 
     { 
      //label1.Content = "rechts"; 
      //scrollviewer1.LineRight(); 
     } 

     private void rectlinks_MouseEnter(object sender, MouseEventArgs e) 
     { 

      //if (rectlinks.IsMouseOver == true) 
      //{ 
      // scrollviewer1.ScrollToHorizontalOffset(scrollviewer1.HorizontalOffset + 10); 
      // label1.Content = "links"; 
      // scrollviewer1.LineLeft(); 
      //} 
     } 


     private Point scrollMousePoint; 
     private bool drag; 

     private void UIElement_OnMouseUp(object sender, MouseButtonEventArgs e) 
     { 
      scrollviewer1.ReleaseMouseCapture(); 

      if (IsMouseOverControl(BtnFoo) && !drag) 
      { 
       var peer = new ButtonAutomationPeer(BtnFoo); 
       var invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider; 

       invokeProv?.Invoke(); 
      } 

      drag = false; 
     } 

     private void UIElement_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 

      scrollMousePoint = e.GetPosition(scrollviewer1); 
      hOff = scrollviewer1.HorizontalOffset; 

      drag = false; 
      scrollviewer1.CaptureMouse(); 
     } 

     private void UIElement_OnPreviewMouseMove(object sender, MouseEventArgs e) 
     { 
      if (scrollviewer1.IsMouseCaptured) 
      { 
       var moveTo = scrollMousePoint.X - e.GetPosition(scrollviewer1).X; 

       if (Math.Abs(moveTo) > 1) 
       { 
        drag = true; 
        scrollviewer1.ScrollToHorizontalOffset(hOff + moveTo); 
       } 


      } 
     } 

     private void ButtonBase_OnClick(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("Click"); 
     } 

     private bool IsMouseOverControl(UIElement control) 
     { 
      var mousePos = Mouse.GetPosition(control); 
      var size = control.RenderSize; 

      if (mousePos.X < 0 || mousePos.X > size.Width || 
       mousePos.Y < 0 || mousePos.Y > size.Height) 
      { 
       return false; 
      } 

      return true; 
     } 
    } 
} 
+0

這是一個很好的解決方案,稍微先進,然後我的技能,所以我會研究所有這些新的事情,謝謝(upvoting) – JohnChris