我在用户控件中有一个关闭按钮。单击时我想在主窗口中隐藏网格 我想通过这个按钮控制
private void PWChangeClose_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation dAnim = new DoubleAnimation();
dAnim.From = 0;
dAnim.To = 120;
dAnim.Duration = TimeSpan.FromSeconds(0.4);
dAnim.EasingFunction = new QuadraticEase();
MainWindow Main = new MainWindow();
Main.grdDockTop.BeginAnimation(HeightProperty, dAnim);
}
但这不起作用 grdDockTop是Mainwindow中的网格。
答案 0 :(得分:1)
您可以通过获取其实例
来访问主窗口属性和字段private void PWChangeClose_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation dAnim = new DoubleAnimation();
dAnim.From = 0;
dAnim.To = 120;
dAnim.Duration = TimeSpan.FromSeconds(0.4);
dAnim.EasingFunction = new QuadraticEase();
//MainWindow Main = new MainWindow(); Change this to:
var Main = App.Current.MainWindow as MainWindow;
Main.grdDockTop.BeginAnimation(HeightProperty, dAnim);
}
答案 1 :(得分:0)
如果你想在没有MVVM模式的代码中执行此操作,则需要访问MainWindow的确切实例。
public class MainWindow
{
public static MainWindow mainWindowInstace;
MainWindow()
{
mainWindowInstace=this;
}
}
并像
一样访问网格MainWindow.mainWindowInstace.grdDockTop