2010-12-23 59 views
9

我想以編程方式使用樣式創建基本的用戶控件。 在這種風格中,我想添加一個Grid(沒問題),但我無法將列定義添加到此網格。以編程方式在WPF中以模板的形式創建網格

我的示例代碼

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; 

FrameworkElementFactoryColumnDefinitionCollection類型的創作會拋出異常"'ColumnDefinitionCollection' type must derive from FrameworkElement, FrameworkContentElement, or Visual3D."

誰能幫我?

+1

也許可以考慮使用`XamlReader.Load()`它是首選了`FrameworkElementFactory` – nan 2010-12-23 09:02:07

回答

5

,你可以簡單地添加列定義這樣

XAML代碼:

<Grid.ColumnDefinitions> 
<ColumnDefinition Height="50"/> 
<ColumnDefinition Height="100"/> 
<ColumnDefinition Height="*"/> 
</Grid.ColumnDefinitions> 

C#代碼:

ColumnDefinition c = new ColumnDefinition(); 
c.Width = new GridLength(50, GridUnitType.Pixel); 

ColumnDefinition c1 = new ColumnDefinition(); 
c1.Width = new GridLength(100, GridUnitType.Pixel); 

ColumnDefinition c2 = new ColumnDefinition(); 
c2.Width = new GridLength(0, GridUnitType.Star); 

grMain.ColumnDefinitions.Add(c); 
grMain.ColumnDefinitions.Add(c1); 
grMain.ColumnDefinitions.Add(c2); 

更多的檢查here

+0

'添加'(而不是'屁股')))) – 2010-12-23 11:51:56

+5

這是行不通的,如果你想把它作爲風格 – wickie79 2010-12-23 15:40:35

2
//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); 
12

FrameworkElementFactory具有一些用於處理Grid中的ColumnDefinitions和RowDefinitions的自定義邏輯。對於那些價值觀,你對待他們像在工廠裏的樹兒,例如:

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); 
1

你只需要改變你的代碼的最後一部分。見下文,

原始代碼:

 colDefinitions.AppendChild(c1); 
     colDefinitions.AppendChild(c2); 
     colDefinitions.AppendChild(c3); 
     mainGrid.AppendChild(colDefinitions); 

新代碼:

 mainGrid.AppendChild(c1); 
     mainGrid.AppendChild(c2); 
     mainGrid.AppendChild(c3); 
相關問題