【问题标题】:How to dynamic add/remove control with Expression Blend?如何使用 Expression Blend 动态添加/删除控件?
【发布时间】:2012-01-19 08:54:53
【问题描述】:

有一阵子,我用 Expression Blend 做了一个演示应用程序。

我的第一个屏幕是一大堆按钮,所以当用户点击任何按钮时,它会转到 MainView。

然后在 MainView 中,我有一个菜单项列表,用户可以单击并显示其相应的 DisplayView。 (约会菜单项将显示 AppointmentView 等)。

一切都很好,我可以点击MenuItem,Views显示动画和过渡效果。

但问题是,通过在 Expression Blend 中创建 MainView、Menu、AppointmentView 等,每件事都是在 XAML 中预定义的。因此,当用户加载第一个屏幕时,必须将所有内容加载到内存中。

现在想想,MainView 等不应该动态添加到屏幕中吗? 我如何使用 Expression Blend 做到这一点?或者唯一的方法就是……在我自己的代码隐藏中执行(为动态添加/删除控件编写 StoryBoard 等?)

如果有任何示例/教程,那就太好了。

【问题讨论】:

    标签: wpf silverlight expression-blend


    【解决方案1】:

    我猜您在不编写代码隐藏的情况下仅在 Blend 中有条件地加载或卸载控件的可能性非常有限。

    一般来说,XAML 中的开始标记相当于某个类对象的无参数构造函数。一旦你写了标签,你就实例化了一个对象,但这并不意味着它的视觉外观被加载到内存中。只有当控件实际显示在屏幕上时才会发生这种情况。

    在我看来,控制某些控件外观的最精简方式是使用单子控件。以 Border 控件为例,将您想要有条件加载的用户控件添加到其子属性中,这样您就可以决定是否加载或卸载控件。

    但不幸的是,我认为您也必须在代码中执行此操作。拿这个简单的代码sn-p:

    // either instantiate in code or use from markuup
    Border myBorder = new Border();
    
    // the control you want to conditionally appear and disappear
    UserControl myUserControl = new UserControl();
    myBorder.Child.Add(myUserControl);
    

    当然,更复杂的方法是使用网格。在这里你必须使用附加属性来添加或删除子元素:

    // either instantiate in code or use from markuup
    Grid myGrid = new Grid();
    
    // the control you want to conditionally appear and disappear
    UserControl myUserControl = new UserControl();
    
    // set the target position inside the Grid via the Grids attached properties
    Grid.setRow(myUserControl, 1);
    Grid.setColumn(myUserControl, 0);
    
    // actually add the control
    Grid.Children.Add(myUserControl);
    

    虽然我很确定你知道所有这些我希望它有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 2013-01-16
      • 2012-12-03
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多