I did it like this: catch the change in the width of UserControl 'a, and start the animation of the width of the content.
In this case, however, the reduced width of UserControl 'and' cuts off 'slowly decreasing parts of the content, so you have to turn off clipping manually.
Control:
<UserControl x:Class="SlowWidthChanging.SlowResizingControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SlowWidthChanging"> <local:NoClipGrid x:Name="InnerPart" HorizontalAlignment="Center" Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"> <Button>Привет, мир!</Button> </local:NoClipGrid> </UserControl>
public partial class SlowResizingControl : UserControl { static DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(SlowResizingControl.WidthProperty, typeof(SlowResizingControl)); public SlowResizingControl() { InitializeComponent(); Loaded += (o, args) => { dpd.AddValueChanged(this, OnWidthChanged); OnWidthChanged(this, null); }; // обязательно отписаться, чтобы избежать утечки памяти Unloaded += (o, args) => dpd.RemoveValueChanged(this, OnWidthChanged); } void OnWidthChanged(object self, EventArgs args) { InnerPart.BeginAnimation( Grid.WidthProperty, new DoubleAnimation( toValue: this.Width, duration: new Duration(TimeSpan.FromSeconds(0.5)), fillBehavior: FillBehavior.Stop)); } }
Well, the auxiliary class ( from here ):
class NoClipGrid : Grid { protected override Geometry GetLayoutClip(Size layoutSlotSize) { return null; } }
You can test:
<Window x:Class="SlowWidthChanging.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SlowWidthChanging" Height="350" Width="525"> <Grid x:Name="OuterContainer"> <local:SlowResizingControl Height="50" Width="100" x:Name="SizeChangeTarget"/> <Button Width="150" Height="25" Content="Поменять ширину" Click="ChangeButtonSize" VerticalAlignment="Bottom" Margin="10"/> </Grid> </Window>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } void ChangeButtonSize(object sender, RoutedEventArgs e) { double newWidth = (double.IsNaN(SizeChangeTarget.Width) || SizeChangeTarget.Width == 100) ? 200 : 100; SizeChangeTarget.Width = newWidth; } }
UserControlsuitable? - VladD