2013-03-27 69 views
1

我已经在Silverlight中为Google FrameworkElementFactory搜索了一下,我们没有这个类,如果没有,我们有其他的替代方法请帮助我。Silverlight5中的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); 

上面的代码工作正常WPF广告应用程式,但现在我想要做的Silverlight5 我使用VS 2010是相同的,Silverlight5 我想补充的DataTemplate动态

回答

3

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。

+0

感谢您的回复,但没有运气。 – Gowtham 2013-04-04 13:57:41

+0

它应该工作...是否有例外? – 2013-04-04 15:38:44

+0

@Siler Solver:它没有抛出异常,但没有显示任何元素..发现不同的解决方案,如Directly创建对象的Eclipse类和填充相应的颜色..感谢您的努力和瓦解时间 – Gowtham 2013-04-09 09:17:43