【问题标题】:Dynamic Control Template in SilverlightSilverlight 中的动态控制模板
【发布时间】:2009-04-25 06:57:26
【问题描述】:

我正在尝试让 Silverlight 中的按钮使用控件模板来更改其外观。

我需要在代码中动态执行此操作(不是 xaml 标记)。 Button 对象有一个 Template 属性,您可以为其分配一个 ControlTemplate。

但是如何将 UI 元素填充到 ControlTemplate 中? (在 WPF 中,有一个 VisualTree 属性,但在 Silverlight 中不存在这样的属性)

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    我不确定这是否有帮助,但以防万一。要在代码后面(不是 XAML)中使用控件模板创建按钮,我已经这样做了:

    1. 从 xml 定义加载控件模板(下面是源链接)

          byte[] bytes = ReadBytesFromStream("BestBuyRemix.BL.buttontemplate.xml");
          string buttonTemplate = "";
          UTF8Encoding encoding = new UTF8Encoding();
          buttonTemplate = encoding.GetString(bytes.ToArray(), 0, (int)bytes.Length);
      
    2. 创建按钮并将其添加到可视化树(在本例中为包装面板)

    string onebutton = string.Format(buttonTemplate, mnu.CatItemName, mnu.CatItemImage, "{StaticResource buttonStyle1}", "{StaticResource CatItemNameBlock}", "{StaticResource ThumbNailPreview}", ictr.ToString()); ictr += 1;

            Button bt = (Button)XamlReader.Load(onebutton);
            bt.Tag = mnu.CatItemPageUri;
            bt.Click += new RoutedEventHandler(bt_Click);
    
            Wrappable.Children.Add(bt);
    

    我在我的博客上写了一篇关于 Best Buy Remix API 的文章,它使用它在详细信息页面中构建产品列表。它有一个指向 Silverlight 源的链接。如果您有兴趣。
    blog post link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多