Creating a Silverlight DataTemplate in code

前端 未结 4 1588
被撕碎了的回忆
被撕碎了的回忆 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:35

    Yes, Silverligt 4 older than WPF's current versions. When you add a template as a resource i.e. as I did I added a userControl Template in Application.xaml MergedResources between ResourceDictionary. In XAML if tag implemented IDictionary you could user x:Key attribute. Like that

       
        
        
        
       
    

    Ok! You may reach your template by coding that, Application.Current.resources["TextBoxEditTemplate"] on the other hand some methods for finding members of this template will not work. Beside this DataTemplate doesn't implement IDictionary so you cannot assign x:Key attribute for items in this dataTemplate. as myOwnControl in example.

    Without xaml current silverlight has some restrictions about creation fully dynamic code-behind DataTemplates.Even it works on WPF.

    Anyway the best solution by this point is creation of XAML script for datatemplate ,You may assing some values element in DataTemplate script. We created our own usercontrols has some properties with DependencyObjectProperty...

    At last if your object has no inherits ,i.e. not a MyControl:UserControl you may inherit MyObject:DependencyObject by this way you can reach your object by calling like Application.Current.Resources.FirstChilderen...

    FindName works only in WPF

提交回复
热议问题