我目前正努力让我的CheckBox
es在GridViewColumn
内正确居中。
我为Style
es定义了CheckBox
,如此:
<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
中,如下所示:
<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保持左对齐(即使调整列的大小)。
有什么想法吗?
提前致谢,
桑尼
编辑:我一直在空白窗口中使用CheckBox
,我认为问题可能与CheckBox
控件有关。如果我制作一个非常广泛的CheckBox
,我仍然无法使CheckBox
部分与其内部对齐。它总是想要左上角。根据名称,ContentAlignment
属性似乎只是对齐内容。
答案 0 :(得分:11)
尝试将HorizontalContentAlignment设置为Stretch for ListViewItem的
<ListView ...>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
<ListView.ItemContainerStyle>
<!-- ... -->
</ListView>
<强>更新强>
这是一个仅限Xaml示例,它以CheckBoxes为中心。粘贴并尝试:)
<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>