Creating a Silverlight DataTemplate in code

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

       <ResourceDictionary>
        <DataTemplate x:Key="TextBoxEditTemplate">
        <Some user control x:Name="myOwnControl" />
        </DataTemplate>
       </ResourceDictionary>
    

    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

    0 讨论(0)
  • 2020-11-29 08:36

    I had a few problems with this code, getting element not foung exceptions. Just for reference, it was that I needed my namesspace included in the DataTemplate...

    private DataTemplate Create(Type type)
            {
                string xaml = @"<DataTemplate 
                    xmlns=""http://schemas.microsoft.com/client/2007""
                    xmlns:controls=""clr-namespace:" + type.Namespace + @";assembly=" + type.Namespace + @""">
                    <controls:" + type.Name + @"/></DataTemplate>";
                return (DataTemplate)XamlReader.Load(xaml);
            }
    
    0 讨论(0)
  • 2020-11-29 08:46

    citation from MSDN:

    The XAML usage that defines the content for creating a data template is not exposed as a settable property. It is special behavior built into the XAML processing of a DataTemplate object element.

    0 讨论(0)
  • 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(
                @"<DataTemplate
                    xmlns=""http://schemas.microsoft.com/client/2007"">
                    <" + type.Name + @"/>
                  </DataTemplate>"
              );
        }
    

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

    0 讨论(0)
提交回复
热议问题