通过Usercontrol对MainForm上的网格边距进行动画处理

时间:2018-09-08 20:55:26

标签: c# wpf data-binding

我在父窗口(仪表板)上有一个带有网格名称=“ page”的用户控件(Menu.xaml)。  我在用户控件上也有一个按钮(ButtonOpenMenu),我想更改主窗体上grid(page)的Margin属性。

所以基本上

在后面的UserControl代码上

   public void ButtonOpenMenu_Click(object sender, RoutedEventArgs e)
        {
         //  change margin of grid on Parent Wndow(Dashboard)

ThicknessAnimation grdanimation =  new ThicknessAnimation(new thickness(0), new Thickness(200, 0, 0, 0),
       new Duration(new TimeSpan(0, 0, 1)), FillBehavior.HoldEnd);
Dashboard db = new Dashbaord();

        db.page.BeginAnimation(Border.MarginProperty, anima);


        } 

有帮助吗?

1 个答案:

答案 0 :(得分:-1)

找到了解决我问题的方法。

所以在我的Menu.xaml(用户控件)中,我创建了一个静态事件

// static
public static event EventHandler AnimateButtonClicked;

  // button click event
public void Animatebutton_Click(object sender, RoutedEventArgs e)
    {
        AnimateButtonClicked(this, e);
    }

然后在MainWindow(仪表板)中。我创建了一个方法(DoAnimate),该方法会在单击用户控件上的按钮(Animatebutton)时触发。

    Dashboard.xaml

    private void DoAnimate(Object sender, EventArgs e)
    { 
        ThicknessAnimation anima =
        new ThicknessAnimation(new Thickness(0), new Thickness(150, 0, 0, 0),
        new Duration(new TimeSpan(0, 0, 1)), FillBehavior.HoldEnd);
        anima.From = new Thickness(150, 0, 0, 0);
        anima.To = new Thickness(100, 0, 0, 0);
        anima.AutoReverse = false;

        // page is the name of the grid that gets animated.
        page.BeginAnimation(Border.MarginProperty, anima);
    }

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {
       Menu.AnimateButtonClicked += new EventHandler(DoAnimate);

    }