【问题标题】:WPF GroupBox Header location in centerWPF GroupBox 标题在中心位置
【发布时间】:2020-04-27 12:43:16
【问题描述】:

我已经编辑了标准 GroupBox 模板,因为我想对其进行自定义。除了其他自定义,我希望 GroupBox 标题在中心而不是左或右水平对齐。 Header 的对齐不是问题,但真正的问题是为 Border 控件定义的 OpacityMask。不透明度蒙版设置不绘制边框的组框标题后面的透明空间。当我将标题设置为中心时,我无法弄清楚如何将透明空间间隙放置在组框标题后面。

<ControlTemplate x:Key="GroupBoxControlTemplate1" TargetType="{x:Type GroupBox}">
    <Grid SnapsToDevicePixels="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="6"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="6"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="6"/>
        </Grid.RowDefinitions>
        <Border Background="{TemplateBinding Background}" BorderBrush="Transparent" 
                BorderThickness="{TemplateBinding BorderThickness}" 
                CornerRadius="4" Grid.Column="1" Grid.ColumnSpan="4" 
                Grid.Row="1" Grid.RowSpan="3" HorizontalAlignment="Stretch"/>
        <Border x:Name="Header" Grid.Column="2" Grid.RowSpan="2" HorizontalAlignment="Left" 
                Padding="3,1,3,0" VerticalAlignment="Stretch">
            <Border.Effect>
                <DropShadowEffect BlurRadius="10" Direction="334"/>
            </Border.Effect>
            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                              Content="{TemplateBinding Header}" 
                              ContentSource="Header" 
                              ContentStringFormat="{TemplateBinding HeaderStringFormat}" 
                              ContentTemplate="{TemplateBinding HeaderTemplate}" 
                              RecognizesAccessKey="True" Height="Auto" 
                              VerticalAlignment="Center"
                              HorizontalAlignment="Center"
                              OpacityMask="#FF3844BD" Margin="0,1,0,0">
            </ContentPresenter>
        </Border>
        <ContentPresenter Margin="{TemplateBinding Padding}" 
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                          Content="{TemplateBinding Content}" 
                          ContentStringFormat="{TemplateBinding ContentStringFormat}" 
                          ContentTemplate="{TemplateBinding ContentTemplate}" 
                          Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2"/>
        <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" 
                CornerRadius="4" Grid.ColumnSpan="3" Grid.Row="1" Grid.RowSpan="3" RenderTransformOrigin="0.5,0.5" Margin="0">
            <Border.OpacityMask>
                <MultiBinding ConverterParameter="7" UpdateSourceTrigger="Default">
                    <MultiBinding.Converter>
                        <BorderGapMaskConverter/>
                    </MultiBinding.Converter>
                    <Binding Path="ActualWidth" ElementName="Header"/>
                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                </MultiBinding>
            </Border.OpacityMask>
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="4">
                <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="4"/>
            </Border>
        </Border>
    </Grid>
</ControlTemplate>

标题文本应该在GroupBox的中心,目前它在左侧,这是GroupBox的默认位置。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    这里通过link指定,需要修改parameter的值。但是ConverterParameter 不是 DependencyProperty,您不能将任何值绑定到它。我建议您编写自己的转换器以将 VisualBrush 返回到您的 GroupBox

    您也可以查看BorderGapMaskConverterhere的源代码

    【讨论】:

      猜你喜欢
      • 2011-01-07
      • 1970-01-01
      • 2011-05-13
      • 2011-06-21
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2010-10-10
      相关资源
      最近更新 更多