2012-07-16 59 views
3

我想要像圖像格式的標題(INWARD在第一行,總重量,純重量&數量在第二行)。我可以用XAML中的以下代碼實現相同的功能,但是如何以編程方式執行此操作?如何將xaml以編程方式轉換?

XAML:

<dg:DataGrid> 
     <dg:DataGridTemplateColumn Width="210"> 
      <dg:DataGridTemplateColumn.HeaderTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition /> 
         </Grid.RowDefinitions> 
         <TextBlock Grid.Row="0" 
            Text="INWARD" 
            TextAlignment="Center"> 
         </TextBlock> 
         <StackPanel Grid.Row="1" Orientation="Horizontal"> 
          <TextBlock Width="80" 
             Text="Gross Weight" 
             TextAlignment="Right" 
             Margin="0,0,2,0"> 
          </TextBlock> 
          <TextBlock Width="80" 
             Text="Pure Weight" 
             TextAlignment="Right" 
             Margin="0,0,0,0"> 
          </TextBlock> 
          <TextBlock Width="40" 
             Text="Quantity" 
             TextAlignment="Right" 
             Margin="2,0,0,0"> 
          </TextBlock> 
         </StackPanel> 
        </Grid> 
       </DataTemplate> 
      </dg:DataGridTemplateColumn.HeaderTemplate> 
      <dg:DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" > 
         <TextBlock Style="{DynamicResource grdCellCurrencyData}" 
                 Width="80" 
                 Margin="0,0,2,0"> 
          <TextBlock.Text> 
           <MultiBinding Converter="{StaticResource CurrencyConverter}" ConverterParameter="True"> 
            <Binding Path="INGrossWeight" Mode="OneWay" /> 
            <Binding Path="BaseUOMNoofDecimals" Mode="OneWay" /> 
           </MultiBinding> 
          </TextBlock.Text> 
         </TextBlock> 
         <TextBlock Style="{DynamicResource grdCellCurrencyData}" 
                  Width="80" 
                  Margin="0,0,0 0"> 
          <TextBlock.Text> 
           <MultiBinding Converter="{StaticResource CurrencyConverter}" ConverterParameter="True"> 
            <Binding Path="INPureWeight" Mode="OneWay" /> 
            <Binding Path="BaseUOMNoofDecimals" Mode="OneWay" /> 
           </MultiBinding> 
          </TextBlock.Text> 
         </TextBlock> 
         <TextBlock Style="{DynamicResource grdCellCurrencyData}" 
            Width="40" 
            Text="{Binding Path=INQuantity, Mode=OneWay}" Margin="2,0,0,0"> 
         </TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </dg:DataGridTemplateColumn.CellTemplate> 
     </dg:DataGridTemplateColumn> 
    </dg:DataGrid> 

在上面的代碼中,如果你看到DataGridTemplateColumn,我已採取網格內,並分爲兩行頭。同樣的方式,我想從後面的代碼編程。任何人都可以幫忙嗎?

回答

0

你可以嘗試使用FrameworkElementFactory以編程方式創建DataTemplate爲你的頭,下面的SO線程具有與代碼 - How do I build a DataTemplate in c# code?

但是,正如FrameworkElementFactorydeprecated,這將是更好地界定在頭模板Resources並使用FindResource()來設置HeaderTemplate。

編輯:

這裏是你的代碼:

DataGridTemplateColumn col1 = new DataGridTemplateColumn(); 

//create the data template 
DataTemplate headerLayout = new DataTemplate(); 

//set up the stack panel 
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid)); 

// define grid's rows 
var row1 = new FrameworkElementFactory(typeof(RowDefinition)); 
gridFactory.AppendChild(row1); 
var row2 = new FrameworkElementFactory(typeof(RowDefinition)); 
gridFactory.AppendChild(row2); 

// set up the inwardTextBlock 
FrameworkElementFactory inwardTextBlock = new FrameworkElementFactory(typeof(TextBlock)); 
inwardTextBlock.SetValue(Grid.RowProperty, 0); 
inwardTextBlock.SetValue(TextBlock.TextProperty, "INWARD"); 
gridFactory.AppendChild(inwardTextBlock); 

//set up the stack panel 
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel)); 
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); 
spFactory.SetValue(Grid.RowProperty, 1); 

// set up the grossWeightTextBlock 
FrameworkElementFactory grossWeightTextBlock = new FrameworkElementFactory(typeof(TextBlock)); 
inwardTextBlock.SetValue(TextBlock.TextProperty, "Gross Weight"); 
inwardTextBlock.SetValue(TextBlock.WidthProperty, 80); 
spFactory.AppendChild(inwardTextBlock); 

// set up the pureWeightTextBlock 
FrameworkElementFactory pureWeightTextBlock = new FrameworkElementFactory(typeof(TextBlock)); 
inwardTextBlock.SetValue(TextBlock.TextProperty, "Pure Weight"); 
inwardTextBlock.SetValue(TextBlock.WidthProperty, 80); 
spFactory.AppendChild(inwardTextBlock); 

// set up the qtyWeightTextBlock 
FrameworkElementFactory qtyWeightTextBlock = new FrameworkElementFactory(typeof(TextBlock)); 
inwardTextBlock.SetValue(TextBlock.TextProperty, "Quantity"); 
inwardTextBlock.SetValue(TextBlock.WidthProperty, 80); 
spFactory.AppendChild(inwardTextBlock); 

gridFactory.AppendChild(spFactory); 

// set the visual tree of the data template 
headerLayout.VisualTree = gridFactory; 

// set the header template 
col1.HeaderTemplate = headerLayout; 
+0

我試圖用戶FrameworkElementFactory但不能夠實現的目標。你能否爲我提供相同的示例代碼。 – Snehal 2012-07-16 12:57:58

+0

@Snehal新增示例代碼(未測試) – akjoshi 2012-07-16 13:20:26

+0

感謝您的示例。我會測試並讓你知道... – Snehal 2012-07-17 05:42:57

相關問題