我创建了一个绑定到数据网格的超级标头网格。在调整datagrid列的大小期间,超级标头会在各自的datagrid列上保持定位。
XAML绑定很棒,一切正常。
唯一的问题是,我需要能够在C#代码中动态构造它。尝试了许多方法,但我无法使绑定工作。
以下XAML的C#等价物是什么
感谢。
<Window x:Class="TESTDataColumnWidthBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="419" Width="827" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
<Grid x:Name="windowGrid">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="SuperHeaderGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=DetailDataGrid, Path=RowHeaderWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column0, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column1, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column2, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column3, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column4, Path=ActualWidth}" />
</Grid.ColumnDefinitions>
<Border Grid.Column="2" Grid.ColumnSpan="5" BorderBrush="Gray" Background="AliceBlue" HorizontalAlignment="Stretch" BorderThickness="1">
<Label>Super Header 1</Label>
</Border>
</Grid>
<Grid Grid.Row="1" x:Name="CategoryHeaderGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=DetailDataGrid, Path=RowHeaderWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column0, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column1, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column2, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column3, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column4, Path=ActualWidth}" />
</Grid.ColumnDefinitions>
<Border Grid.Column="2" Grid.ColumnSpan="2" BorderBrush="Gray" Background="Beige" HorizontalAlignment="Stretch" BorderThickness="1">
<Label>Cat 1</Label>
</Border>
<Border Grid.Column="4" Grid.ColumnSpan="2" BorderBrush="Gray" Background="AntiqueWhite" HorizontalAlignment="Stretch" BorderThickness="1">
<Label>Cat 2</Label>
</Border>
</Grid>
<my:DataGrid Grid.Column="0" Grid.ColumnSpan="15" Grid.Row="3" x:Name="DetailDataGrid" ItemsSource="{Binding Path=.}" AlternatingRowBackground="AliceBlue">
<my:DataGrid.Columns>
<my:DataGridTextColumn x:Name="Column0" Header="ID" />
<my:DataGridTextColumn x:Name="Column1" Header="Count1" />
<my:DataGridTextColumn x:Name="Column2" Header="Count2" />
<my:DataGridTextColumn x:Name="Column3" Header="Count3" />
<my:DataGridTextColumn x:Name="Column4" Header="Count4" />
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
答案 0 :(得分:0)
应该相当于:
绑定cdBinding = new Binding(“RowHeaderWidth”); cdBinding.Source = DetailDataGrid;
ColumnDefinition colDef = new ColumnDefinition(); colDef.SetBinding(ColumnDefinition.WidthProperty,cdBinding);
SuperHeaderGrid.ColumnDefinitions.Add(colDef);
类似于其他列定义。