13,347
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="CommonWPFApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CommonWPFApplication"
MouseMove="Grid_MouseMove"
Title="MainWindow" Height="350" Width="525">
<UserControl x:Class="CommonWPFApplication.UserControl1"
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="300" d:DesignWidth="300">
<Grid Background="Cyan">
<TextBlock FontSize="36">111111</TextBlock>
</Grid>
</UserControl>
<UserControl x:Class="CommonWPFApplication.UserControl2"
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="300" d:DesignWidth="300">
<Grid Background="Violet">
<TextBlock FontSize="36">22222</TextBlock>
</Grid>
</UserControl>
<Window x:Class="CommonWPFApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CommonWPFApplication"
MouseMove="Grid_MouseMove"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="GridContent" >
<local:UserControl1 x:Name="UserControlPart1">
<local:UserControl1.RenderTransform>
<TransformGroup>
<TranslateTransform ></TranslateTransform>
</TransformGroup>
</local:UserControl1.RenderTransform>
</local:UserControl1>
<local:UserControl2 x:Name="UserControlPart2" Visibility="Collapsed">
<local:UserControl2.RenderTransform>
<TransformGroup>
<TranslateTransform ></TranslateTransform>
</TransformGroup>
</local:UserControl2.RenderTransform>
</local:UserControl2>
</Grid>
</Window>
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;
using System.Collections.ObjectModel;
using System.Windows.Media.Animation;
namespace CommonWPFApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private double? _lastX=null;
private double? _lastY=null;
private bool _stopAnimated = false;
public MainWindow()
{
InitializeComponent();
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
if (_stopAnimated)
{
return;
}
Point p = e.GetPosition(this);
if (_lastX == null )
{
_lastX = p.X;
_lastY = p.Y;
return;
}
if (e.LeftButton != MouseButtonState.Pressed)
{
return;
}
if (_lastX.HasValue)
{
if (Math.Abs(p.X - _lastX.Value) >= 100)
{
DoubleAnimation da = new DoubleAnimation(0, 800, TimeSpan.FromMilliseconds(250));
TranslateTransform tt = GetTranslateTransform(UserControlPart1);
if (tt != null)
{
da.Completed += (o1, e1) =>
{
UserControlPart1.Visibility = Visibility.Collapsed;
UserControlPart2.Visibility = Visibility.Visible;
DoubleAnimation da1 = new DoubleAnimation(-800, 0, TimeSpan.FromMilliseconds(250));
tt = GetTranslateTransform(UserControlPart2);
if (tt != null)
{
tt.BeginAnimation(TranslateTransform.XProperty, da1);
}
};
tt.BeginAnimation(TranslateTransform.XProperty, da);
}
_lastX = null;
_stopAnimated = true;
}
}
}
private TranslateTransform GetTranslateTransform(Control c)
{
TransformGroup tg = c.RenderTransform as TransformGroup;
foreach (var item in tg.Children)
{
if (item is TranslateTransform)
return item as TranslateTransform;
}
return null;
}
}
}
<Grid Width="500" Height="300">
<InkCanvas>
</InkCanvas>
</Grid>