2017-07-28 58 views
1

我正在使用基本的WPF數據網格。我需要添加超級​​首標。每個超級標題具有兩列的跨度。此外,這必須動態創建,因爲列數是根據數據庫中的數據確定的。我看起來像這樣:SuperHeader。問題是,如果我調整列的大小,超級鏈接器不按預期移動。下面是代碼: MainWindow.xaml:WPF Datagrid Superheader

<TabItem Header="Tab4"> 
       <StackPanel> 
        <Grid Name="grdSuperHeader"/> 
        <DataGrid Name="grdSample" ScrollViewer.HorizontalScrollBarVisibility="Disabled" AutoGenerateColumns="False"> 
        <DataGrid.Columns> 
          <DataGridTextColumn x:Name="SampleColumn1" Header="Column 1"></DataGridTextColumn> 
          <DataGridTextColumn x:Name="SampleColumn2" Header="Column 2"></DataGridTextColumn> 
          <DataGridTextColumn x:Name="SampleColumn3" Header="Column 3" ></DataGridTextColumn> 
          <DataGridTextColumn x:Name="SampleColumn4" Header="Column 4"></DataGridTextColumn> 
          <DataGridTextColumn x:Name="SampleColumn5" Header="Column 5"></DataGridTextColumn> 
          <DataGridTextColumn x:Name="SampleColumn6" Header="Column 6"></DataGridTextColumn> 
         </DataGrid.Columns> 
        <TextBlock></TextBlock> 
        <TextBlock></TextBlock> 
        <TextBlock></TextBlock> 
        <TextBlock></TextBlock> 
        <TextBlock></TextBlock> 
       </DataGrid> 
       <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> 
         <TextBlock Width="{Binding ElementName=SampleColumn1,Path=ActualWidth}" Text="Footer1" Margin="5,0,0,0"></TextBlock> 
         <TextBlock Width="{Binding ElementName=SampleColumn2,Path=ActualWidth}" Text="Footer2" Margin="5,0,0,0"></TextBlock> 
         <TextBlock Width="{Binding ElementName=SampleColumn3,Path=ActualWidth}" Text="Footer3" Margin="5,0,0,0"></TextBlock> 
         <TextBlock Width="{Binding ElementName=SampleColumn4,Path=ActualWidth}" Text="Footer4" Margin="5,0,0,0"></TextBlock> 
         <TextBlock Width="{Binding ElementName=SampleColumn5,Path=ActualWidth}" Text="Footer5" Margin="5,0,0,0"></TextBlock> 
         <TextBlock Width="{Binding ElementName=SampleColumn6,Path=ActualWidth}" Text="Footer6" Margin="5,0,0,0"></TextBlock> 
        </StackPanel> 
       </StackPanel> 
      </TabItem> 

Mainwindow.xaml.cs:

private void addSuperHeader() 
{ 
    //Create the columns 
    for (int i= 0;i< grdSample.Columns.Count;i++) 
    { 
     ColumnDefinition col = new ColumnDefinition(); 
     grdSuperHeader.ColumnDefinitions.Add(col); 

     var binding = new Binding("ActualWidth") 
     { 
      ElementName = "SampleColumn" + i.ToString() 
     }; 

     col.SetBinding(ColumnDefinition.WidthProperty, binding); 

     if(i%2==0) 
     { 


      //Add header text 
      TextBlock txtHeader = new TextBlock(); 
      txtHeader.Text = "Cols["+(i+1).ToString()+"-"+(i+2).ToString()+"]"; 
      txtHeader.Foreground = new SolidColorBrush(Colors.Green); 
      txtHeader.HorizontalAlignment = HorizontalAlignment.Center; 
      Grid.SetRow(txtHeader, 0); 
      Grid.SetColumn(txtHeader, i); 
      Grid.SetColumnSpan(txtHeader, 2); 

      grdSuperHeader.Children.Add(txtHeader); 
     } 

    } 

} 

我在做什麼錯?

+0

您後面的代碼沒有任何datagrid列寬度的信息,那麼您如何期望隨列更改寬度? –

回答

1

我認爲這是GridSplittersBindingActualWidthWidth的問題。但是,您可以對您的Binding使用以下解決方法。

ColumnDefinition col = new ColumnDefinition {Width = GridLength.Auto}; 
    grdSuperHeader.ColumnDefinitions.Add(col); 

    var binding = new Binding("ActualWidth") 
    { 
     ElementName = "SampleColumn" + i.ToString() 
    }; 

    col.SetBinding(ColumnDefinition.MaxWidthProperty, binding); 
    col.SetBinding(ColumnDefinition.MinWidthProperty, binding); 

綁定ActalWidthMinWidthMaxWidth並設置ColumnDefinition Width="Auto"。這在我的例子中工作。

+0

謝謝WPF德國。你提出的改變當然有幫助。但是,超級首標沒有像預期那樣獲得兩列的跨度。你能檢查我最初發布在if(i%2 == 0)塊內的代碼段嗎? –

+0

列跨度問題也是固定的,與您發佈的代碼部分無關。 –

+0

不客氣。我在寬度同步和網格劃分方面遇到了幾乎相同的問題。此解決方法是與網格一起工作的唯一解決方案,由用戶調整大小。 – WPFGermany