关闭

WPF多表头的伪实现

698人阅读 评论(0) 收藏 举报

在做WPF的时候遇到了这样一个问题:


要实现这样的一个两个header的datagrid,在微软论坛上找到了一个老外的解决办法——用一个Grid做底,上面可以用一些TextBlock填充,下面填充一个dataGrid,在Grid.ColumnDefinitions的时候,将宽度绑定到下面dataGrid的实际宽度上,就可以很好地实现上面的功能


<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding ElementName=Column1,Path=ActualWidth}"></ColumnDefinition>
            <ColumnDefinition Width="{Binding ElementName=Column2,Path=ActualWidth}"></ColumnDefinition>
            <ColumnDefinition Width="{Binding ElementName=Column3,Path=ActualWidth}"></ColumnDefinition>
            <ColumnDefinition Width="{Binding ElementName=Column4,Path=ActualWidth}"></ColumnDefinition>
            <ColumnDefinition Width="{Binding ElementName=Column5,Path=ActualWidth}"></ColumnDefinition>
            <ColumnDefinition Width="{Binding ElementName=Column6,Path=ActualWidth}"></ColumnDefinition>
            <ColumnDefinition Width="{Binding ElementName=Column7,Path=ActualWidth}"></ColumnDefinition>
            <ColumnDefinition Width="{Binding ElementName=Column8,Path=ActualWidth}"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="2" Grid.ColumnSpan="2" BorderBrush="AliceBlue" BorderThickness="3">
        <TextBlock  TextAlignment="Center">范围一</TextBlock>
        </Border>
        <Border Grid.Column="4" Grid.ColumnSpan="2" BorderBrush="AliceBlue" BorderThickness="3">
            <TextBlock  TextAlignment="Center">范围二</TextBlock>
        </Border>
        <Border Grid.Column="6" Grid.ColumnSpan="2" BorderBrush="AliceBlue" BorderThickness="3">
            <TextBlock  TextAlignment="Center">范围三</TextBlock>
        </Border>
        <DataGrid Grid.ColumnSpan="8" Grid.Row="1" AutoGenerateColumns="False"
                                Name="datagrid1"  RowHeaderWidth="0">
            <DataGrid.Columns>
                <DataGridTextColumn Header="column 1"  x:Name="Column1" />
                <DataGridTextColumn Header="column 2"  x:Name="Column2"/>
                <DataGridTextColumn Header="column 3"  x:Name="Column3"/>
                <DataGridTextColumn Header="column 4"  x:Name="Column4"/>
                <DataGridTextColumn Header="column 1"  x:Name="Column5" />
                <DataGridTextColumn Header="column 2"  x:Name="Column6"/>
                <DataGridTextColumn Header="column 3"  x:Name="Column7"/>
                <DataGridTextColumn Header="column 4"  x:Name="Column8"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7311次
    • 积分:299
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:0篇
    • 译文:1篇
    • 评论:0条
    文章分类