c# - Animate 如何改变 wpf 中的 ColumnDefinition 宽度?

标签 c# wpf

我需要通过单击按钮来更改列的宽度,并且此更改应该是缓慢且动画的。

  <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" x:Name="Col1"/>
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>

        <Button >change Width Col1</Button>
  </Grid>

最佳答案

试试这个:

Xaml:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" x:Name="Col1"/>
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>

    <Button Click="Button_Click">change Width Col1</Button>
</Grid>

代码隐藏:

  private void Button_Click(object sender, RoutedEventArgs e)
  {
        Storyboard storyboard = new Storyboard();

        Duration duration = new Duration(TimeSpan.FromMilliseconds(2000));
        CubicEase ease = new CubicEase { EasingMode = EasingMode.EaseOut };

        DoubleAnimation animation = new DoubleAnimation();
        animation.EasingFunction = ease;
        animation.Duration = duration;
        storyboard.Children.Add(animation);
        animation.From = 1000;
        animation.To = 0;            
        Storyboard.SetTarget(animation, Col1);
        Storyboard.SetTargetProperty(animation, new PropertyPath("(ColumnDefinition.MaxWidth)"));

        storyboard.Begin();
  }

关于c# - Animate 如何改变 wpf 中的 ColumnDefinition 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45586324/

相关文章:

c# - 在没有存储过程的情况下从 SQL Server 表生成 C# 类

c# - 当 "break all"没有响应时,如何调试无响应的 .Net 服务?

c# - 绑定(bind)实际上是如何工作的?

.net - 在 C# 中从 SQLite 中提取的最快方法

c# - 使用 Roslyn 时,控制台不包含 ReadKey 的定义

c# - 遍历泛型类的集合属性

c# - 使用现有的数据库名称验证实现 OWIN?

.net - 文本框的值消失 - 将 View 模型绑定(bind)到选项卡(内容控件)

wpf - .net 4.5 不适用于 VS2010,未找到 INotifyDataErrorInfo

wpf - 如何将 TextBlock 绑定(bind)到包含格式化文本的资源?