У меня получилось следующим образом: ловить изменение ширины UserControl
'а, и запускать анимацию ширины контента.
При этом, правда, уменьшившаяся ширина UserControl
'а «отрежет» медленно уменьшающиеся части контента, так что придётся отключить clipping вручную.
Контрол:
<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));
}
}
Ну и вспомогательный класс (отсюда):
class NoClipGrid : Grid
{
protected override Geometry GetLayoutClip(Size layoutSlotSize)
{
return null;
}
}
Можно тестировать:
<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;
}
}
UserControl
?