【问题标题】:Is there any way to add checkbox to all column headers inside datagrid in wpf有没有办法在 wpf 中的 datagrid 内的所有列标题中添加复选框
【发布时间】:2014-02-05 01:57:33
【问题描述】:

我有一个包含数据网格的列表框,它通过以下方式绑定到数据表:

在后面的代码中:

listBox1.Items.Add(dt1);

这里的 dt 是表名。

在 Xaml 中:

<ListBox Grid.Row="0" MinHeight="305" HorizontalAlignment="Stretch"  Name="listBox1" VerticalAlignment="Stretch" MinWidth="537" >            
            <ListBox.ItemTemplate>                
                <DataTemplate>                    
                        <StackPanel MinHeight="80" MinWidth="500" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="40"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <DataGrid Grid.Column="1" AutoGenerateColumns="True" MinHeight="75" HorizontalAlignment="Center" Name="dataGrid1" VerticalAlignment="Stretch" MinWidth="470" MaxWidth="900" ItemsSource="{Binding}" IsReadOnly="True" >
                            </DataGrid>
                            <CheckBox  Grid.Column="0" Height="35" Width="25" Name="IsDone" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                            </Grid>                        
                    </StackPanel>                   
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox> 

我想为数据网格中的所有列标题添加复选框。您能否建议我如何为 Wpf 中数据网格中存在的所有列标题添加复选框

提前致谢

【问题讨论】:

  • 我不能将它完全标记为重复,但你会得到关于仅使用 HeaderStyle 的 jist here

标签: wpf xaml datagrid


【解决方案1】:

您可以为您的 DataGrid 定义 ColumnHeaderStyle 并在其中设置 ContentTemplate 为所有标题设置复选框,如下所示:

    <DataGrid>
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <CheckBox Content="{Binding}"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>

【讨论】:

    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    相关资源
    最近更新 更多