居中在GridViewColumn中的复选框? [英] Centering a checkbox in a GridViewColumn?
问题描述
我目前正在努力获取我的 CheckBox
es,以正确地位于我的 GridViewColumn
中。
I'm currently struggling on getting my CheckBox
es to properly center within my GridViewColumn
s.
我为我的 CheckBox
es定义了一个样式
:
I've defined a Style
for my CheckBox
es like so:
<Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Margin" Value="4" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" />
</Style>
而我的 CheckBox
es被添加到 GridViewColumn
使用 DataTemplate
如下:
And my CheckBox
es are added into the GridViewColumn
using a DataTemplate
like this:
<GridViewColumn Header="Comment">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource DataGridCheckBox}" IsChecked="{Binding PropertyItem.Comment, Converter={StaticResource booleanConverter}, ConverterParameter='string'}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
但我遇到的问题是 CheckBox
es保持左对齐(即使调整列的大小)。
But the problem I have is that the CheckBox
es remain left-aligned (even when resizing the column).
任何想法?
提前感谢,
Sonny
Thanks in advance,
Sonny
编辑:我一直在乱写一个 CheckBox
在空白的窗口,我认为这个问题可能与 CheckBox
控件有关。如果我做了一个非常广泛的 CheckBox
,我仍然似乎无法获得 CheckBox
本身。它总是想去左上角。按照名称, ContentAlignment
属性似乎只是将内容对齐。
I've been messing around with a CheckBox
in a blank window and I think the problem may be related to the CheckBox
control. If I make a very wide CheckBox
, I still can't seem to get the CheckBox
portion of it to align within itself. It always wants to go to the upper-left. As per the name, the ContentAlignment
properties only seem to align the content.
推荐答案
尝试为ListViewItem的
Try to set HorizontalContentAlignment to Stretch for the ListViewItem's
<ListView ...>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
<ListView.ItemContainerStyle>
<!-- ... -->
</ListView>
更新
这是一个仅限Xaml的例子,它将CheckBox中心。粘贴并尝试:)
Here's a Xaml only example which centers the CheckBoxes. Paste it and try it :)
<Grid>
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.Resources>
<Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Margin" Value="4" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Comment">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource DataGridCheckBox}" IsChecked="True"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListViewItem>Item1</ListViewItem>
<ListViewItem>Item2</ListViewItem>
<ListViewItem>Item3</ListViewItem>
</ListView>
</Grid>
这篇关于居中在GridViewColumn中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!