我正在使用基本的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);
}
}
}
我在做什麼錯?
您後面的代碼沒有任何datagrid列寬度的信息,那麼您如何期望隨列更改寬度? –