简体   繁体   English

WPF-Groupbox标头对齐

[英]WPF - Groupbox header alignment

in a WPF app, I want to achieve something like this for my groupbox: 在WPF应用中,我想为我的分组框实现以下功能:

目标

The goal is creating a template for the header, just to display something in the left (checkbox + text), and something in the right side (button). 目标是为页眉创建一个模板,仅在左侧(复选框+文本)显示某些内容,在右侧(按钮)显示某些内容。

Unfortunately, the only I get is: 不幸的是,我唯一得到的是:

what I get http://imageshack.com/a/img513/706/fiaq.png 我得到了什么http://imageshack.com/a/img513/706/fiaq.png

I would like not to do it programmatically, but everything in the XAML (as cleaner as possible). 我不希望以编程方式执行此操作,而是希望XAML中的所有内容(尽可能简洁)。 I mean, using grids, columndefinitions, etc... not dealing with margins that will crash everything when resizing the window. 我的意思是,使用网格,columndefinitions等...不处理在调整窗口大小时会使所有内容崩溃的边距。

The XAML I'm using (grid with three columns): 我正在使用的XAML(带有三列的网格):

<GroupBox Margin="5">
        <GroupBox.Header>
            <Grid>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="15"></ColumnDefinition>
                    <ColumnDefinition Width="100*"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <CheckBox Grid.Column="0"></CheckBox>
                <TextBlock Grid.Column="1" Margin="5,0,5,0" Text="This is my groupbox"></TextBlock>
                <Button Content="Click" FontSize="8" Background="Yellow" Grid.Column="2" Height="16" Width="54"></Button>

            </Grid>
        </GroupBox.Header>
    </GroupBox>

What do you think? 你怎么看?

By default header is left align for GroupBox header. 默认情况下,标题与GroupBox标题保持左对齐。 But in your case you want few parts to be left aligned and few to be right for which you need to override ControlTemplate of groupBox header like defined here and here . 但是,在您的情况下,您希望保留很少的部分,而希望保留的几乎是正确的部分,则需要覆盖groupBox标头的ControlTemplate ,如此此处所定义。


However, you can force the Grid to stretch horizontally so that button can be placed in right. 但是,您可以强制Grid to stretch horizontally沿Grid to stretch horizontally以便可以将按钮置于右侧。 But as you can see in snapshot, border line gets hide for header content so you might get something like this: 但是,正如您在快照中看到的那样,边框线会隐藏标题内容,因此您可能会得到以下内容:

在此处输入图片说明

Relevant code to achieve this will be: 实现此目的的相关代码为:

XAML XAML

<GroupBox Margin="5" x:Name="groupBox"
          xmlns:local="clr-namespace:NamespaceOfConverter">
    <GroupBox.Resources>
        <local:SubtractionConverter x:Key="SubtractionConverter"/>
    </GroupBox.Resources>
    <GroupBox.Header>
        <Grid Width="{Binding ActualWidth, ElementName=groupBox,
                      Converter={StaticResource SubtractionConverter}}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="15"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <CheckBox Grid.Column="0"/>
            <TextBlock Grid.Column="1" Margin="5,0,5,0"
                       Text="This is my groupbox"/>
            <Button Content="Click" FontSize="8" Background="Yellow"
                    HorizontalAlignment="Right" Grid.Column="2"
                    Height="16" Width="54"/>
        </Grid>
    </GroupBox.Header>
</GroupBox>

Converter 变流器

public class SubtractionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
                          System.Globalization.CultureInfo culture)
    {
        return (double)value - 25.0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
                              System.Globalization.CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM