开发者

Binding Super Header Columns to DataGrid Column Width in Code

开发者 https://www.devze.com 2022-12-21 02:19 出处:网络
I\'ve created a grid of super headers that bind to a datagrid. The super headers stay positioned over their respective datagrid columns during resizing of datagrid columns.

I've created a grid of super headers that bind to a datagrid. The super headers stay positioned over their respective datagrid columns during resizing of datagrid columns.

The XAML binding is great and everything works fine.

The only problem however, is that I need to be able to construct this dynamically in C# code. Tried a number of approaches, but I can't get the binding to work.

What is the C# equivalent of the following XAML

Thanks.

<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>


should be equivalent to:

Binding cdBinding = new Binding("RowHeaderWidth"); cdBinding.Source = DetailDataGrid;

ColumnDefinition colDef = new ColumnDefinition(); colDef.SetBinding(ColumnDefinition.WidthProperty, cdBinding);

SuperHeaderGrid.ColumnDefinitions.Add(colDef);

And similarly for the other column definitions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号