Je veux créer un contrôle utilisateur de base avec un style de manière programmatique.
Dans ce style, je veux ajouter un Mon exemple de code est P> grille code> (pas de problème), mais je ne peux pas ajouter de définitions de colonne à cette grille.
ControlTemplate templ = new ControlTemplate();
FrameworkElementFactory mainPanel = new FrameworkElementFactory(typeof(DockPanel));
mainPanel.SetValue(DockPanel.LastChildFillProperty, true);
FrameworkElementFactory headerPanel = new FrameworkElementFactory(typeof(StackPanel));
headerPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
headerPanel.SetValue(DockPanel.DockProperty, Dock.Top);
mainPanel.AppendChild(headerPanel);
FrameworkElementFactory headerImg = new FrameworkElementFactory(typeof(Image));
headerImg.SetValue(Image.MarginProperty, new Thickness(5));
headerImg.SetValue(Image.HeightProperty, 32d);
headerImg.SetBinding(Image.SourceProperty, new Binding("ElementImage") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
headerPanel.AppendChild(headerImg);
FrameworkElementFactory headerTitle = new FrameworkElementFactory(typeof(TextBlock));
headerTitle.SetValue(TextBlock.FontSizeProperty, 16d);
headerTitle.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
headerTitle.SetBinding(TextBlock.TextProperty, new Binding("Title") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
headerPanel.AppendChild(headerTitle);
FrameworkElementFactory mainGrid = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory c1 = new FrameworkElementFactory(typeof(ColumnDefinition));
c1.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Star));
FrameworkElementFactory c2 = new FrameworkElementFactory(typeof(ColumnDefinition));
c2.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Auto));
FrameworkElementFactory c3 = new FrameworkElementFactory(typeof(ColumnDefinition));
c3.SetValue(ColumnDefinition.WidthProperty, new GridLength(3, GridUnitType.Star));
FrameworkElementFactory colDefinitions = new FrameworkElementFactory(typeof(ColumnDefinitionCollection));
colDefinitions.AppendChild(c1);
colDefinitions.AppendChild(c2);
colDefinitions.AppendChild(c3);
mainGrid.AppendChild(colDefinitions);
mainPanel.AppendChild(mainGrid);
FrameworkElementFactory content = new FrameworkElementFactory(typeof(ContentPresenter));
content.SetBinding(ContentPresenter.ContentProperty, new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Path = new PropertyPath("Content") });
mainGrid.AppendChild(content);
templ.VisualTree = mainPanel;
Style mainStyle = new Style();
mainStyle.Setters.Add(new Setter(UserControl.TemplateProperty, templ));
this.Style = mainStyle;
4 Réponses :
Vous pouvez simplement ajouter des définitions de colonne comme celle-ci
pour plus de chèque ici p> p>
Les définitions de colonne ont eu la largeur, pas la hauteur :)
//create grid var grid = new FrameworkElementFactory(typeof(Grid)); // assign template to grid CellControlTemplate.VisualTree = grid; // define grid's rows var r = new FrameworkElementFactory(typeof(RowDefinition)); grid.AppendChild(r); // define grid's columns var c = new FrameworkElementFactory(typeof(ColumnDefinition)); grid.AppendChild(c); c = new FrameworkElementFactory(typeof(ColumnDefinition)); c.SetValue(ColumnDefinition.WidthProperty, GridLength.Auto); grid.AppendChild(c); c = new FrameworkElementFactory(typeof(ColumnDefinition)); c.SetValue(ColumnDefinition.WidthProperty, GridLength.Auto); grid.AppendChild(c);
FrameworcyElementFactory a une logique personnalisée pour la gestion des colonnesDéfinitions et des rowDéfinitions dans une grille. Pour ces valeurs, vous les traitez comme des enfants dans l'arbre d'usine, par exemple:
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid)); var column1 = new FrameworkElementFactory(typeof(ColumnDefinition)); column1.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Auto)); var column2 = new FrameworkElementFactory(typeof(ColumnDefinition)); column2.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Star)); gridFactory.AppendChild(column1); gridFactory.AppendChild(column2);
Il vous suffit de changer la dernière partie de votre code. Voir ci-dessous,
code d'origine: p> nouveau code: p>
Toujours agréable d'expliquer pourquoi le changement est nécessaire!
Peut-être envisager d'utiliser
xamlreader.load () code> il est préféré sur
FramewradElementFactory code>