8,735
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="WpfApp1.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition x:Name="MenuList" Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Name="cmd"
Grid.Column="0"
Click="Button_Click" />
<TextBlock Grid.Column="1"
Background="Blue" />
<GridSplitter
Grid.Column="2"
Width="3"
HorizontalAlignment="Stretch"
DragCompleted="GridSplitter_DragCompleted" />
<TextBlock Grid.Column="3"
Background="Red" />
</Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace WpfApp1
{
public partial class MainWindow : Window
{
int PanelWidth = 200;
public MainWindow()
{
InitializeComponent();
MenuList.Width = new GridLength(PanelWidth);
}
private void slideSubMenu(DependencyObject animateObject, GridLength from, GridLength to)
{
Storyboard storyboard = new Storyboard();
GridLengthAnimation widthAnimation = new GridLengthAnimation()
{
From = from,
To = to,
Duration = new Duration(TimeSpan.FromSeconds(0.3))
};
Storyboard.SetTarget(widthAnimation, animateObject);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("(Width)"));
storyboard.Children.Add(widthAnimation);
storyboard.Begin();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
bool bSubMenuVisible = (MenuList.Width == new GridLength(PanelWidth));
double toGridLength = bSubMenuVisible ? 0 : PanelWidth;
slideSubMenu(MenuList, MenuList.Width, new GridLength(toGridLength));
//如果屏蔽上面那句,启用下面那句,则 GridSplitter 能正常拖动
//MenuList.Width = new GridLength(toGridLength);
}
private void GridSplitter_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
PanelWidth = (int)MenuList.ActualWidth;
}
}
}
using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace WpfApp1
{
public class GridLengthAnimation : AnimationTimeline
{
public override Type TargetPropertyType => typeof(GridLength);
protected override Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation));
public GridLength From
{
get
{
return (GridLength)GetValue(FromProperty);
}
set
{
SetValue(FromProperty, value);
}
}
public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation));
public GridLength To
{
get
{
return (GridLength)GetValue(ToProperty);
}
set
{
SetValue(ToProperty, value);
}
}
public const string EasingFunctionPropertyName = "EasingFunction";
public IEasingFunction EasingFunction
{
get
{
return (IEasingFunction)GetValue(EasingFunctionProperty);
}
set
{
SetValue(EasingFunctionProperty, value);
}
}
public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register(
EasingFunctionPropertyName,
typeof(IEasingFunction),
typeof(GridLengthAnimation),
new UIPropertyMetadata(null));
public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
{
double fromVal = ((GridLength)GetValue(FromProperty)).Value;
double toVal = ((GridLength)GetValue(ToProperty)).Value;
double progress = animationClock.CurrentProgress.Value;
IEasingFunction easingFunction = EasingFunction;
if (easingFunction != null)
{
progress = easingFunction.Ease(progress);
}
if (fromVal > toVal)
{
progress = 1 - progress;
}
double v = progress * Math.Abs(fromVal - toVal) + Math.Min(fromVal, toVal);
return new GridLength(v, GridUnitType.Pixel);
}
}
}