2012-07-16 3 views
3

이미지에서와 같이 형식 헤더를 원합니다 (첫 줄에 입력, 총 중량, 두 번째 줄에 순수한 & 수량). 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에, 내가 안으로 그리드를 촬영하고 두 행에서 헤더를 분리 참조합니다. 코드에서 프로그래밍 방식으로 수행하려는 것과 같은 방식입니다. 아무도 도와 줄 수 있니?

당신은 SO 스레드 데 그와 관련된 다음 코드를 시도하고, 프로그램 헤더에 대한 DataTemplate을 만들 FrameworkElementFactory을 사용할 수 있습니다

답변

0

- FrameworkElementFactorydeprecated이기 때문에,에서 헤더 템플릿을 정의하는 것이 좋습니다 것, How do I build a DataTemplate in c# code?

그러나 ResourcesFindResource()을 사용하여 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

+0

@ Snnehal 샘플 코드 추가 (테스트되지 않음) – akjoshi

+0

샘플 주셔서 감사합니다. 나는 시험 할 것이고, 당신에게 알릴 것이다. ... – Snehal

관련 문제