【问题标题】:How to Center a Checkbox in a XAML DataGrid如何在 XAML DataGrid 中将复选框居中
【发布时间】:2017-04-11 02:10:18
【问题描述】:

我正在尝试将复选框置于数据网格单元格的中心。

我似乎无法想出一种在不同 Windows 主题中工作的方法来使其居中。例如,我可以将它居中,这样它在经典 Windows 主题中看起来不错,但在 Windows 7 Basic 主题下它是关闭的。

<DataGridCheckBoxColumn ElementStyle="{StaticResource CenteredCheckStyle}" MinWidth="15" 
CellStyle="{StaticResource SingleClickEditing}"  Visibility="{Binding exists}"
Binding="{Binding Path=toTransfer, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" 
CanUserSort="False" CanUserResize="false" CanUserReorder="false">
     <DataGridCheckBoxColumn.HeaderTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                <CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"  
                       HorizontalContentAlignment="Center" HorizontalAlignment="Center"
                       IsThreeState="False" Margin="8,0,0,0" Loaded="CheckBox_Loaded"/>
             </StackPanel>
         </DataTemplate>
    </DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>

【问题讨论】:

  • 不要使用那个保证金 8,0,0,0...
  • 如果我去掉 Margin,它在经典中看起来不错,如果我添加它,它在 Windows 7 Basic 中看起来不错
  • 如果你不需要所有 DataGrid 的东西,你可以试试 'HeadedItemsControl',stackoverflow.com/questions/32453131/…

标签: wpf xaml checkbox datagrid


【解决方案1】:

我认为您可以在下面设置 DG 的 HeaderStyle 属性。 我也是 WPF 新手,不是专家,但希望它在某种程度上对您有所帮助。

<DataGridCheckBoxColumn.HeaderStyle>
    <Style TargetType="{x:Type DataGridColumnHeader}">
       <Setter Property="HorizontalContentAlignment" Value="Center" />
    </Style>
</DataGridCheckBoxColumn.HeaderStyle>

【讨论】:

    猜你喜欢
    • 2012-12-17
    • 1970-01-01
    • 2013-01-28
    • 2011-06-20
    • 1970-01-01
    • 2022-11-15
    • 2016-01-25
    • 2019-05-29
    • 2015-04-03
    相关资源
    最近更新 更多