首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FrameworkElementFactory在Silverlight5中的什么位置

FrameworkElementFactory在Silverlight5中的什么位置
EN

Stack Overflow用户
提问于 2013-03-27 22:04:02
回答 1查看 896关注 0票数 1

我已经在谷歌上搜索了Silverlight中的FrameworkElementFactory,我们没有这个类吗,如果没有,我们还有其他选择吗?请帮助我。

代码语言:javascript
复制
  FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Grid));
                spFactory.Name = "myComboFactory";
                spFactory.SetValue(Grid.WidthProperty, Convert.ToDouble(3));
                spFactory.SetValue(Grid.HeightProperty, Convert.ToDouble(3));
                spFactory.SetValue(Grid.RenderTransformProperty, new TranslateTransform(-6, -6));


                FrameworkElementFactory ec1 = new FrameworkElementFactory(typeof(Ellipse));
                ec1.SetValue(Ellipse.FillProperty, Brushes.Red);
                spFactory.AppendChild(ec1);

上面的代码运行得很好,但是现在我想在Silverlight5中做同样的事情,我使用的是VS2010,Silverlight5我想动态添加DataTemplate

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-28 04:13:07

FrameworkElementFactory在Silverlight中不存在。如果要在运行时生成DataTemplates,则必须使用XamlReader类。

对于您的情况,您可能会这样做:

代码语言:javascript
复制
ListBox listbox = new ListBox();
DataTemplate template = System.Windows.Markup.XamlReader.Load(
    @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
        <Grid Width=""3"" Height=""3"">
            <Grid.RenderTransform>
                <TranslateTransform X=""6"" Y=""6"" />
            </Grid.RenderTransform>
            <Ellipse Fill=""Red"" />
        </Grid>
    </DataTemplate>") as DataTemplate;

listbox.ItemTemplate = template;

请注意,您必须在根元素(xmlns=...)中定义默认名称空间。

同样有趣的是,您可以(必须)使用此方法以编程方式设置ItemsControl的ItemsPanel。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15660843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档