Creating a Silverlight DataTemplate in code

前端 未结 4 1596
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 08:08

How do I create a silverlight data template in code? I\'ve seen plenty of examples for WPF, but nothing for Silverlight.

Edit: Here\'s the code I\'m now using this f

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 08:59

    Although you cannot programatically create it, you can load it from a XAML string in code like this:

        public static DataTemplate Create(Type type)
        {
            return (DataTemplate) XamlReader.Load(
                @"
                    <" + type.Name + @"/>
                  "
              );
        }
    

    The snippet above creates a data template containing a single control, which may be a user control with the contents you need.

提交回复
热议问题