2010-12-23 25 views
9

के रूप में डब्ल्यूपीएफ में एक ग्रिड बनाएं मैं प्रोग्राम शैली के साथ एक बुनियादी उपयोगकर्ता नियंत्रण बनाना चाहता हूं। इस शैली में मैं Grid (कोई समस्या नहीं) जोड़ना चाहता हूं, लेकिन मैं इस ग्रिड में कॉलम परिभाषा नहीं जोड़ सकता।टेम्पलेट प्रोग्रामेटिक रूप से

मेरे उदाहरण कोड

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; 

है लेकिन प्रकार ColumnDefinitionCollection साथ FrameworkElementFactory के निर्माण के लिए एक अपवाद "'ColumnDefinitionCollection' type must derive from FrameworkElement, FrameworkContentElement, or Visual3D."

कौन मेरी मदद कर सकते फेंक होगा?

+1

हो सकता है कि प्रयोग करने पर विचार 'XamlReader.Load()' 'इस पर FrameworkElementFactory' पसंद है – nan

उत्तर

5

आप बस इस

XAML कोड की तरह स्तंभ परिभाषाएँ जोड़ सकते हैं:

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

सी # कोड: अधिक जांच करने के लिए

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

'जोड़ें' (नहीं ' गधा '))) –

+5

यदि आप इसे शैली – wickie79

+0

के रूप में बनाना चाहते हैं तो यह काम नहीं करता है कॉलम परिभाषा चौड़ाई, ऊंचाई नहीं है :) – boski

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

हमेशा यह बताने के लिए अच्छा है कि परिवर्तन की आवश्यकता क्यों है! – sharakan

संबंधित मुद्दे