我已经在谷歌上搜索了Silverlight中的FrameworkElementFactory,我们没有这个类吗,如果没有,我们还有其他选择吗?请帮助我。
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
发布于 2013-03-28 04:13:07
FrameworkElementFactory在Silverlight中不存在。如果要在运行时生成DataTemplates,则必须使用XamlReader类。
对于您的情况,您可能会这样做:
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。
https://stackoverflow.com/questions/15660843
复制相似问题