2017-01-03 79 views
0

我需要帮助更正拖动后我的拇指拖动或我的动画。我基本上要的步骤是:在滚动柄WPF C#动画大拇指

  1. 用户左键点击,在窗口拖动手柄+图像 水平。
  2. 手柄+图像要么捕捉到左边的窗口边缘,或手柄 将捕捉到右边的窗口边缘与隐藏到 权
  3. 如果用户释放左键的同时拖动图像时, 手柄+图像将继续沿着其朝向左边或右边窗口边缘的方向 并与其对齐,cf #2

现在,加载后,它正在按照我的要求工作,但对于随后的拖动,拇指拖动可见性消失了。它只是跳到我释放左键单击并从那里继续捕捉动画的位置。

我已经尝试了各种解决方法,尽我所能,但似乎没有解决方案。最小代码如下。

MainWindow.xaml

<Window x:Class="TouchSample.MainWindow" 
     Name="Window_TouchSample" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:TouchSample" 
     mc:Ignorable="d" 
     Title="MainWindow" 
    Height="600" Width="600" 
    xmlns:custom="clr-namespace:TouchSample"> 
    <Grid Name="Grid_TouchableThing"> 
     <custom:TouchableThing /> 
    </Grid> 
</Window> 

TouchableThing.xaml

<UserControl x:Class="TouchSample.TouchableThing" 
     Name="Scroll_UserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:TouchSample" 
     mc:Ignorable="d" 
     d:DesignHeight="600" d:DesignWidth="600"> 
    <Canvas x:Name="Scroll_Canvas"> 

     <Thumb Name="Scroll_thumb" 
       Canvas.Left="20" 
       Canvas.Top="120" 
       Canvas.ZIndex="3" 
       DragDelta="Thumb_DragDelta" 
       PreviewMouseLeftButtonUp="Scroll_thumb_PreviewMouseLeftButtonUp"> 
      <Thumb.Template> 
       <ControlTemplate> 
        <StackPanel Name="Scroll_thumb_StackPanel" 
           Orientation="Horizontal"> 
         <Border Name="Scroll_thumb_StackPanel_Border" Background="Black" Padding="1"> 
          <TextBlock Name="Scroll_thumb_StackPanel_Border_TextBlock" 
             Text="Scroll" 
             Background="Silver" 
             TextAlignment="Center" 
             MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" 
             > 
           <TextBlock.LayoutTransform> 
            <RotateTransform Angle="90"/> 
           </TextBlock.LayoutTransform> 
          </TextBlock> 
         </Border> 
         <Button Background="Yellow" Padding="1"> 
          <Image Name="Scroll_thumb_StackPanel_Button_Image" Source="Assets/long-ancient-scroll-4499037_x4.jpg" /> 
         </Button> 
        </StackPanel> 
       </ControlTemplate> 
      </Thumb.Template> 
     </Thumb> 

    </Canvas> 
</UserControl> 

TouchableThing.xaml.cs

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Controls.Primitives; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 

namespace TouchSample 
{ 
    /// <summary> 
    /// Interaction logic for TouchableThing.xaml 
    /// </summary> 
    public partial class TouchableThing : UserControl 
    { 
     private double toLeftValue = 0; 
     private double toRightValue = 550; // will get overwritten in OnWindowSizeChanged 
     private bool isGoingLeft = false; 
     private bool isGoingRight = false; 
     private double lastDiffX; 

     public TouchableThing() 
     { 
      InitializeComponent(); 
      this.SizeChanged += OnWindowSizeChanged;    
     } 

     private void OnWindowSizeChanged(object sender, SizeChangedEventArgs e) 
     { 
      //throw new NotImplementedException(); 

      //Console.WriteLine("sender: " + sender.ToString()); 
      //Console.WriteLine("sender.GetType(): " + sender.GetType().ToString()); 
      //Console.WriteLine("e: " + e.ToString()); 

      toRightValue = e.NewSize.Width; 

      var aBorder = VisualHierarchyHelper.FindChild<Border>(Scroll_thumb, "Scroll_thumb_StackPanel_Border"); 

      if (aBorder != null) 
      { 
       Console.WriteLine("aBorder.ActualWidth: " + aBorder.ActualWidth.ToString()); 
       Console.WriteLine("aBorder.RenderSize.Height: " + aBorder.RenderSize.Height.ToString()); 
       toRightValue -= aBorder.ActualWidth; 
      } 

      Console.WriteLine("toRightValue=" + toRightValue.ToString()); 
     } 

     // https://wpf.2000things.com/2012/12/19/715-using-the-thumb-control-to-drag-objects-on-a-canvas/ 
     private void Thumb_DragDelta(object sender, DragDeltaEventArgs e) 
     { 
      //UIElement thumb = e.Source as UIElement; 
      UIElement thumb = sender as UIElement; 

      double xChange = Canvas.GetLeft(thumb) + e.HorizontalChange; 

      if(xChange >= toLeftValue && xChange <= toRightValue) 
      { 
       Canvas.SetLeft(thumb, xChange); 
       //Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange); 

       //Console.WriteLine("e.HorizontalChange=" + e.HorizontalChange); 
       if (e.HorizontalChange > 0) 
       { 
        isGoingLeft = false; 
        isGoingRight = true; 
       } 
       else if (e.HorizontalChange < 0) 
       { 
        isGoingLeft = true; 
        isGoingRight = false; 
       } 
       else 
       { 
        isGoingLeft = false; 
        isGoingRight = false; 
       } 

       lastDiffX = xChange; 
      } 
     } 

     private void goLeft(UIElement theUIElement, double fromX) 
     { 
      DoubleAnimation goLeftAnim = new DoubleAnimation(); 

      goLeftAnim.From = fromX; 
      goLeftAnim.To = toLeftValue; 
      goLeftAnim.Duration = new Duration(TimeSpan.FromSeconds(2)); 

      theUIElement.BeginAnimation(Canvas.LeftProperty, goLeftAnim); 
     } 

     private void goRight(UIElement theUIElement, double fromX) 
     { 
      DoubleAnimation goRightAnim = new DoubleAnimation(); 

      goRightAnim.From = fromX; 
      goRightAnim.To = toRightValue; 
      goRightAnim.Duration = new Duration(TimeSpan.FromSeconds(2)); 

      theUIElement.BeginAnimation(Canvas.LeftProperty, goRightAnim); 
     } 

     private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      TextBlock aTextBlock = sender as TextBlock; 

      //Console.WriteLine("TextBlock_MouseLeftButtonDown: Changing TextBlock Background colour to Gray"); 
      aTextBlock.Background = Brushes.Gray; 
     } 

     private void Scroll_thumb_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
     { 
      var aTextBlock = VisualHierarchyHelper.FindChild<TextBlock>(Scroll_thumb, "Scroll_thumb_StackPanel_Border_TextBlock"); 

      if (Mouse.LeftButton == MouseButtonState.Released && aTextBlock != null) 
      { 
       //Console.WriteLine("Scroll_thumb_PreviewMouseLeftButtonUp: Changing TextBlock Background colour to Silver"); 
       //Console.WriteLine(" found aTextBlock"); 
       aTextBlock.Background = Brushes.Silver; 
      } 

      if (isGoingLeft) 
       goLeft(Scroll_thumb, lastDiffX); 
      else if (isGoingRight) 
       goRight(Scroll_thumb, lastDiffX); 

     } 

    } 
} 

我刚开始用WPF只用了一年的关于C#经验和WinForms,所以任何帮助非常感谢。

Sryn

回答