居中在GridViewColumn中的复选框? [英] Centering a checkbox in a GridViewColumn?

查看:103
本文介绍了居中在GridViewColumn中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力获取我的 CheckBox es,以正确地位于我的 GridViewColumn 中。

I'm currently struggling on getting my CheckBoxes to properly center within my GridViewColumns.

我为我的 CheckBox es定义了一个样式

I've defined a Style for my CheckBoxes 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 CheckBoxes 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 CheckBoxes 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 CheckBoxin a blank window and I think the problem may be related to the CheckBoxcontrol. If I make a very wide CheckBox, I still can't seem to get the CheckBoxportion 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆