private void AddDataTemplate(Type viewType, Type viewModelType) { var dataTemplate = new DataTemplate(viewModelType); dataTemplate.VisualTree = new FrameworkElementFactory(viewType); dataTemplate.Seal(); App.Current.Resources.Add(new DataTemplateKey(viewModelType), dataTemplate); }
/// <summary> /// Override this method to return an app specific <seealso cref="DataTemplate"/>. /// </summary> /// <param name="item">The data content</param> /// <param name="container">The container in which the content is to be displayed</param> /// <returns>a app specific template to apply.</returns> public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (SystemXmlHelper.IsXmlNode(item)) { if (_xmlNodeContentTemplate == null) { _xmlNodeContentTemplate = new DataTemplate(); FrameworkElementFactory text = ContentPresenter.CreateTextBlockFactory(); Binding binding = new Binding(); binding.XPath = _displayMemberPath; binding.StringFormat = _stringFormat; text.SetBinding(TextBlock.TextProperty, binding); _xmlNodeContentTemplate.VisualTree = text; _xmlNodeContentTemplate.Seal(); } return _xmlNodeContentTemplate; } else { if (_clrNodeContentTemplate == null) { _clrNodeContentTemplate = new DataTemplate(); FrameworkElementFactory text = ContentPresenter.CreateTextBlockFactory(); Binding binding = new Binding(); binding.Path = new PropertyPath(_displayMemberPath); binding.StringFormat = _stringFormat; text.SetBinding(TextBlock.TextProperty, binding); _clrNodeContentTemplate.VisualTree = text; _clrNodeContentTemplate.Seal(); } return _clrNodeContentTemplate; } }
/// <summary> /// Returns the cell data template displaying the member path in a text block. /// </summary> /// <param name="pDisplayMemberPath">The display member path.</param> /// <returns>The built data template.</returns> public System.Windows.DataTemplate GetCellTemplate(string pDisplayMemberPath) { // Trying to get it from the cache. System.Windows.DataTemplate lDataTemplate; if (this.mCellDataTemplates.TryGetValue(pDisplayMemberPath, out lDataTemplate)) { return(lDataTemplate); } if (string.IsNullOrEmpty(pDisplayMemberPath) == false) { // Building dynamically the data template. FrameworkElementFactory lTextBlockFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock)); Binding lDisplayMemberBinding = new Binding(pDisplayMemberPath); lTextBlockFactory.SetBinding(System.Windows.Controls.TextBlock.TextProperty, lDisplayMemberBinding); lDataTemplate = new System.Windows.DataTemplate(); lDataTemplate.VisualTree = lTextBlockFactory; lDataTemplate.Seal(); // Caching the data template. this.mCellDataTemplates.Add(pDisplayMemberPath, lDataTemplate); return(lDataTemplate); } // Getting the bind to source data template. return(this.mCellDataTemplates[BindToSourceCellDataTemplateKey]); }
/// <summary> /// Initializes the <see cref="TypeBasedDataTemplateSelector"/> class. /// </summary> static ModelItemTemplateSelector() { DefaultDataTemplate = new DataTemplate(); var factory = new FrameworkElementFactory(typeof(TextBlock)); var propertyPath = new Binding() { StringFormat = "Cannot find template for type {0}. Optionally set the DataTemplate's x:Key property is set with the full name of this type." }; factory.SetBinding(TextBlock.TextProperty, propertyPath); factory.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); factory.SetValue(TextBlock.ForegroundProperty, Brushes.Red); DefaultDataTemplate.VisualTree = factory; DefaultDataTemplate.Seal(); }
/// <summary> /// Initializes the static version of <see cref="TransitionElement"/>. /// </summary> static TransitionElement() { // TraceSwitches.Transitions.Level = TraceLevel.Verbose; defaultNullContentTemplate = new DataTemplate(); FrameworkElementFactory rectangle = new FrameworkElementFactory(typeof(Rectangle)); rectangle.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch); rectangle.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Stretch); rectangle.SetValue(Shape.FillProperty, SystemColors.WindowBrush /*new TemplateBindingExtension(Control.ForegroundProperty)?*/); defaultNullContentTemplate.VisualTree = rectangle; defaultNullContentTemplate.Seal(); NullContentTemplateProperty.OverrideMetadata(typeof(TransitionElement), new FrameworkPropertyMetadata(defaultNullContentTemplate)); ClipToBoundsProperty.OverrideMetadata(typeof(TransitionElement), new FrameworkPropertyMetadata(null, CoerceClipToBounds)); }
private static void InitializeStringContentTemplate() { DataTemplate template; FrameworkElementFactory text; // Default template for strings template = new DataTemplate(); text = new FrameworkElementFactory(typeof(TextBlock)); text.SetValue(TextBlock.TextProperty, new TemplateBindingExtension(ContentPresenter.ContentProperty)); template.VisualTree = text; template.Seal(); s_StringTemplate = template; }
protected override void OnViewTemplateChanged(DataTemplate oldValue, DataTemplate newValue) { base.OnViewTemplateChanged(oldValue, newValue); if(newValue != null) newValue.Seal(); }
private static DataTemplate CreateDefaultSpacerTemplate() { FrameworkElementFactory feFactory = new FrameworkElementFactory(typeof(VerticalConnector)); DataTemplate dt = new DataTemplate() { VisualTree = feFactory }; dt.Seal(); return dt; }
private static void BuildDefaultTemplates() { //Defining the FrameworkElementFactory variables I will use below. FrameworkElementFactory hglip; FrameworkElementFactory glip; FrameworkElementFactory border; FrameworkElementFactory contentPresenter; //Defining the Converters, Bindings and ViewBindings I will use to build the templates below. Converters.ThicknessConverter thicknessConverter = new Xceed.Wpf.DataGrid.Converters.ThicknessConverter(); ViewBindingExtension borderThicknessBinding = new ViewBindingExtension( "HorizontalGridLineThickness" ); borderThicknessBinding.Converter = thicknessConverter; borderThicknessBinding.ConverterParameter = Converters.ThicknessConverter.ThicknessSides.Top; //ViewBindingExtension borderBrushBinding = new ViewBindingExtension( "HorizontalGridLineBrush" ); Binding detailTitleBinding = new Binding(); detailTitleBinding.Path = new PropertyPath( "(0).SourceDetailConfiguration.Title", DataGridControl.DataGridContextProperty ); detailTitleBinding.RelativeSource = new RelativeSource( RelativeSourceMode.Self ); Binding detailTitleTemplateBinding = new Binding(); detailTitleTemplateBinding.Path = new PropertyPath( "(0).SourceDetailConfiguration.TitleTemplate", DataGridControl.DataGridContextProperty ); detailTitleTemplateBinding.RelativeSource = new RelativeSource( RelativeSourceMode.Self ); //Defining the Header Spacer template. DefaultHeaderSpacerTemplate = new DataTemplate(); DefaultHeaderSpacerTemplate.VisualTree = new FrameworkElementFactory( typeof( DockPanel ) ); DefaultHeaderSpacerTemplate.VisualTree.SetValue( RowSelector.VisibleProperty, false ); hglip = new FrameworkElementFactory( typeof( HierarchicalGroupLevelIndicatorPane ) ); hglip.SetValue( DockPanel.DockProperty, Dock.Left ); hglip.SetValue( TableView.CanScrollHorizontallyProperty, false ); DefaultHeaderSpacerTemplate.VisualTree.AppendChild( hglip ); glip = new FrameworkElementFactory( typeof( GroupLevelIndicatorPane ) ); glip.SetValue( GroupLevelIndicatorPane.IndentedProperty, false ); glip.SetValue( DockPanel.DockProperty, Dock.Left ); glip.SetValue( GroupLevelIndicatorPane.ShowIndicatorsProperty, false ); glip.SetValue( GroupLevelIndicatorPane.ShowVerticalBorderProperty, false ); glip.SetValue( GroupLevelIndicatorPane.GroupLevelProperty, 0 ); glip.SetValue( TableView.CanScrollHorizontallyProperty, false ); DefaultHeaderSpacerTemplate.VisualTree.AppendChild( glip ); border = new FrameworkElementFactory( typeof( Border ) ); border.SetBinding( Border.BorderThicknessProperty, ( BindingBase )borderThicknessBinding.ProvideValue( null ) ); //border.SetBinding( Border.BorderBrushProperty, ( BindingBase )borderBrushBinding.ProvideValue( null ) ); border.SetValue( Border.MinHeightProperty, 5d ); border.SetValue( TableView.CanScrollHorizontallyProperty, false ); contentPresenter = new FrameworkElementFactory( typeof( ContentPresenter ) ); contentPresenter.SetBinding( ContentPresenter.ContentProperty, detailTitleBinding ); contentPresenter.SetBinding( ContentPresenter.ContentTemplateProperty, detailTitleTemplateBinding ); contentPresenter.SetValue( Control.FontSizeProperty, 14d ); border.AppendChild( contentPresenter ); DefaultHeaderSpacerTemplate.VisualTree.AppendChild( border ); DefaultHeaderSpacerTemplate.Seal(); //Defining the ColumnManagerRow template DefaultColumnManagerRowTemplate = new DataTemplate(); DefaultColumnManagerRowTemplate.VisualTree = new FrameworkElementFactory( typeof( ColumnManagerRow ) ); //DefaultColumnManagerRowTemplate.VisualTree.SetValue( ColumnManagerRow.BackgroundProperty, null ); DefaultColumnManagerRowTemplate.Seal(); DefaultGroupByControlTemplate = new DataTemplate(); DefaultGroupByControlTemplate.VisualTree = new FrameworkElementFactory( typeof( HierarchicalGroupByControl ) ); DefaultGroupByControlTemplate.Seal(); }
static ValueLineConfigurator() { ComboEnumDescriptionTemplate = new DataTemplate { VisualTree = new FrameworkElementFactory(typeof(TextBlock)) .Do(f => f.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right)) .Do(f => f.SetBinding(TextBlock.TextProperty, new Binding() { Mode = BindingMode.OneTime, Converter = Converters.EnumDescription })) }; ComboEnumDescriptionTemplate.Seal(); }
static ContentPresenter() { ContentPresenter.ContentTemplateProperty = ContentControl.ContentTemplateProperty.AddOwner(typeof(ContentPresenter), new PropertyMetadata(new ContentPresenter.DefaultTemplate(), new PropertyChangedCallback(ContentPresenter.OnContentTemplateChanged))); ContentPresenter.RecognizesAccessKeyProperty = DependencyProperty.Register("RecognizesAccessKey", typeof(bool), typeof(ContentPresenter), new PropertyMetadata(BooleanBoxes.FalseBox)); ContentPresenter.ContentProperty = ContentControl.ContentProperty.AddOwner(typeof(ContentPresenter), new PropertyMetadata(null, new PropertyChangedCallback(ContentPresenter.OnContentChanged))); ContentPresenter.ContentTemplateSelectorProperty = ContentControl.ContentTemplateSelectorProperty.AddOwner(typeof(ContentPresenter), new PropertyMetadata(null, new PropertyChangedCallback(ContentPresenter.OnContentTemplateSelectorChanged))); ContentPresenter.ContentStringFormatProperty = DependencyProperty.Register("ContentStringFormat", typeof(string), typeof(ContentPresenter), new PropertyMetadata(null, new PropertyChangedCallback(ContentPresenter.OnContentStringFormatChanged))); ContentPresenter.ContentSourceProperty = DependencyProperty.Register("ContentSource", typeof(string), typeof(ContentPresenter), new PropertyMetadata("Content")); ContentPresenter.TemplateProperty = DependencyProperty.Register("Template", typeof(DataTemplate), typeof(ContentPresenter), new PropertyMetadata(null, new PropertyChangedCallback(ContentPresenter.OnTemplateChanged))); /* //ContentPresenter.XMLFormattingTemplateField = new UncommonField<DataTemplate>(); //ContentPresenter.StringFormattingTemplateField = new UncommonField<DataTemplate>(); //ContentPresenter.AccessTextFormattingTemplateField = new UncommonField<DataTemplate>(); */ DataTemplate dataTemplate = new DataTemplate(); FrameworkElementFactory frameworkElementFactory; /* //FrameworkElementFactory frameworkElementFactory = ContentPresenter.CreateAccessTextFactory(); //frameworkElementFactory.SetValue(AccessText.TextProperty, new TemplateBindingExtension(ContentPresenter.ContentProperty)); //dataTemplate.VisualTree = frameworkElementFactory; //dataTemplate.Seal(); //ContentPresenter.s_AccessTextTemplate = dataTemplate; //dataTemplate = new DataTemplate(); */ frameworkElementFactory = ContentPresenter.CreateTextBlockFactory(); frameworkElementFactory.SetValue(TextBlock.TextProperty, new TemplateBindingExtension(ContentPresenter.ContentProperty)); dataTemplate.VisualTree = frameworkElementFactory; dataTemplate.Seal(); ContentPresenter.stringTemplate = dataTemplate; /* //dataTemplate = new DataTemplate(); //frameworkElementFactory = ContentPresenter.CreateTextBlockFactory(); //Binding binding = new Binding(); //binding.XPath = "."; //frameworkElementFactory.SetBinding(TextBlock.TextProperty, binding); //dataTemplate.VisualTree = frameworkElementFactory; //dataTemplate.Seal(); //ContentPresenter.s_XmlNodeTemplate = dataTemplate; //dataTemplate = new ContentPresenter.UseContentTemplate(); //dataTemplate.Seal(); */ frameworkElementFactory = new FrameworkElementFactory(typeof(ContentControl)); frameworkElementFactory.SetValue(ContentControl.ContentProperty, new TemplateBindingExtension(ContentPresenter.ContentProperty)); dataTemplate = new DataTemplate(); dataTemplate.VisualTree = frameworkElementFactory; ContentPresenter.uiElementTemplate = dataTemplate; dataTemplate = new ContentPresenter.DefaultTemplate(); dataTemplate.Seal(); ContentPresenter.defaultTemplate = dataTemplate; ContentPresenter.defaultTemplateSelector = new ContentPresenter.DefaultSelector(); }
private static DataTemplate CreateDefaultTemplate( Type type, TypeConverter converter ) { Debug.Assert( type != null ); Debug.Assert( converter != null ); var template = new DataTemplate(); var factory = new FrameworkElementFactory( typeof( TextBlock ) ); var binding = new Binding(); binding.Mode = BindingMode.OneWay; binding.Converter = new DefaultConverter( converter ); binding.ConverterCulture = CultureInfo.CurrentCulture; factory.SetBinding( TextBlock.TextProperty, binding ); template.VisualTree = factory; template.Seal(); return template; }
private DataTemplate CreateDataTemplate(Color color) { #if SILVERLIGHT return (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""> <Border BorderThickness=""1 1 0 0"" BorderBrush=""LightGray"" Background=""" + color.ToString() + @"""> <TextBlock Text=""{Binding Data, Mode=OneWay}"" Margin=""4"" VerticalAlignment=""Center"" HorizontalAlignment=""Right""/> </Border> </DataTemplate>" ); #else FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border)); border.SetValue(Border.BorderThicknessProperty, new Thickness(1, 1, 0, 0)); border.SetValue(Border.BorderBrushProperty, Brushes.LightGray); border.SetValue(Border.BackgroundProperty, new SolidColorBrush(color)); DataTemplate dataTemplate = new DataTemplate(); dataTemplate.VisualTree = border; FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock)); textBlock.SetBinding(TextBlock.TextProperty, new Binding("Data")); textBlock.SetValue(TextBlock.MarginProperty, new Thickness(2)); textBlock.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); textBlock.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right); border.AppendChild(textBlock); dataTemplate.Seal(); return dataTemplate; #endif }
private void RefreshDefaultScrollTipContentTemplate() { DataGridContext itemContext = DataGridControl.GetDataGridContext( this ); if( itemContext == null ) return; DataGridControl parentGridControl = itemContext.DataGridControl; if( ( parentGridControl == null ) || ( parentGridControl.ScrollViewer == null ) ) return; ColumnCollection columnCollection = itemContext.Columns; if( columnCollection == null ) return; Column mainColumn = columnCollection.MainColumn as Column; if( mainColumn == null ) return; Xceed.Wpf.DataGrid.Views.UIViewBase uiViewBase = parentGridControl.GetView() as Xceed.Wpf.DataGrid.Views.UIViewBase; if( uiViewBase == null ) return; // The ScrollTip.ContentTemplate will now be set. This is to avoid // a null ContentTemplate when the ColumnsCollection update its // MainColumn after the template is applied this.ScrollTipContentTemplateNeedsRefresh = false; ForeignKeyConfiguration configuration = mainColumn.ForeignKeyConfiguration; // Do not create default template only when none was created before and a template already exists if( ( !this.UsingDefaultScrollTipContentTemplate ) && ( uiViewBase.ScrollTipContentTemplate != null ) ) { if( configuration != null ) { this.ContentTemplate = configuration.DefaultScrollTipContentTemplate; } else { // Clear the value in case we previously affected it this.ClearValue( ScrollTip.ContentTemplateProperty ); // Set the default Binding values this.SetBinding( ScrollTip.ContentTemplateProperty, this.DefaultScrollTipContentTemplateBinding ); this.SetBinding( ScrollTip.ContentTemplateSelectorProperty, this.DefaultScrollTipContentTemplateSelectorBinding ); } } else { // A default ContentTemplate template is created using MainColumn as displayed data this.UsingDefaultScrollTipContentTemplate = true; // If a configuration was found, the default ContentTemplate will // be used to convert Content to Foreign value and // it will use the ScrollTipContentTemplate defined on UIViewBase // if any if( configuration == null ) { // Disable warning for DisplayMemberBinding when internaly used #pragma warning disable 618 BindingBase displayMemberBinding = mainColumn.DisplayMemberBinding; #pragma warning restore 618 FrameworkElementFactory contentPresenter = new FrameworkElementFactory( typeof( ContentPresenter ) ); contentPresenter.SetValue( ContentPresenter.NameProperty, "defaultScrollTipDataTemplateContentPresenter" ); contentPresenter.SetBinding( ContentPresenter.ContentProperty, displayMemberBinding ); contentPresenter.SetBinding( ContentPresenter.ContentTemplateProperty, this.DefaultCellContentTemplateBinding ); contentPresenter.SetBinding( ContentPresenter.ContentTemplateSelectorProperty, this.DefaultCellContentTemplateSelectorBinding ); DataTemplate template = new DataTemplate(); template.VisualTree = contentPresenter; template.Seal(); this.ContentTemplate = template; } else { this.SetBinding( ContentPresenter.ContentTemplateProperty, this.DefaultCellContentTemplateBinding ); this.SetBinding( ContentPresenter.ContentTemplateSelectorProperty, this.DefaultCellContentTemplateSelectorBinding ); this.ContentTemplate = configuration.DefaultScrollTipContentTemplate; } } }
/// <summary> /// Creates a bound data template. /// </summary> /// <param name="dataTemplate">The data template.</param> /// <param name="boundType">Type of the bound object.</param> /// <param name="baseName">A base name.</param> /// <returns></returns> public static IBoundElement DataTemplate(DataTemplate dataTemplate, BoundType boundType, string baseName) { if(!dataTemplate.IsSealed) dataTemplate.Seal(); return new DataTemplateElement(dataTemplate, boundType, baseName); }