Create DataTemplate in code behind

前端 未结 3 764
野的像风
野的像风 2020-11-27 05:12

How do i add controls to datatemplates programmatically?

For Example. Below I\'ve created TextBlock and DataTemplate.

TextBlock text = new TextBlock(         


        
3条回答
  •  眼角桃花
    2020-11-27 05:36

    I know it is an work-around, but I published a tip in code project (http://www.codeproject.com/Tips/808808/Create-Data-and-Control-Templates-using-Delegates) that allows you to create a data-template using a delegate. For example:

    TemplateGenerator.CreateDataTemplate(() => new TextBox());
    

    This will be enough to create a datatemplate that creates a new textbox. If you want a binding too, it could be written like:

    TemplateGenerator.CreateDataTemplate
    (
      () =>
      {
        var result = new TextBox();
        result.SetBinding(TextBox.TextProperty, "PathForTheBinding");
        return result;
      }
    );
    

    The code of the TemplateGenerator is the following:

    /// 
    /// Class that helps the creation of control and data templates by using delegates.
    /// 
    public static class TemplateGenerator
    {
      private sealed class _TemplateGeneratorControl:
        ContentControl
      {
        internal static readonly DependencyProperty FactoryProperty = DependencyProperty.Register("Factory", typeof(Func), typeof(_TemplateGeneratorControl), new PropertyMetadata(null, _FactoryChanged));
    
        private static void _FactoryChanged(DependencyObject instance, DependencyPropertyChangedEventArgs args)
        {
          var control = (_TemplateGeneratorControl)instance;
          var factory = (Func)args.NewValue;
          control.Content = factory();
        }
      }
    
      /// 
      /// Creates a data-template that uses the given delegate to create new instances.
      /// 
      public static DataTemplate CreateDataTemplate(Func factory)
      {
        if (factory == null)
          throw new ArgumentNullException("factory");
    
        var frameworkElementFactory = new FrameworkElementFactory(typeof(_TemplateGeneratorControl));
        frameworkElementFactory.SetValue(_TemplateGeneratorControl.FactoryProperty, factory);
    
        var dataTemplate = new DataTemplate(typeof(DependencyObject));
        dataTemplate.VisualTree = frameworkElementFactory;
        return dataTemplate;
      }
    
      /// 
      /// Creates a control-template that uses the given delegate to create new instances.
      /// 
      public static ControlTemplate CreateControlTemplate(Type controlType, Func factory)
      {
        if (controlType == null)
          throw new ArgumentNullException("controlType");
    
        if (factory == null)
          throw new ArgumentNullException("factory");
    
        var frameworkElementFactory = new FrameworkElementFactory(typeof(_TemplateGeneratorControl));
        frameworkElementFactory.SetValue(_TemplateGeneratorControl.FactoryProperty, factory);
    
        var controlTemplate = new ControlTemplate(controlType);
        controlTemplate.VisualTree = frameworkElementFactory;
        return controlTemplate;
      }
    }
    
    
    

    And it has a method for ControlTemplates too.

    提交回复
    热议问题