WPF自定义GroupBox 标题文字居中显示

5 篇文章 0 订阅
1 篇文章 0 订阅
文章展示了如何在WPF中创建一个名为UGroupBox的自定义控件,该控件扩展了GroupBox并添加了HeaderHorizontalAlignment依赖属性,允许用户设置标题的水平对齐方式。通过注册DependencyProperty并在样式模板中绑定,实现了标题的动态对齐效果。示例代码包括UGroupBox类的定义、资源样式布局和控件的使用方法。
摘要由CSDN通过智能技术生成

实现效果如图:

实现思路:为GroupBox添加HorizontalAlignment 的依赖属性

1、UGroupBox.cs

using MotionControls.Extensions.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace YOI.Controls
{

    public class UGroupBox : GroupBox
    {
        static UGroupBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UGroupBox), new FrameworkPropertyMetadata(typeof(UGroupBox)));
        }


        public HorizontalAlignment HeaderHorizontalAlignmentProperty
        {
            get { return (HorizontalAlignment)GetValue(HeaderHorizontalAlignmentPropertyProperty); }
            set { SetValue(HeaderHorizontalAlignmentPropertyProperty, value); }
        }

        public static readonly DependencyProperty HeaderHorizontalAlignmentPropertyProperty =
            DependencyProperty.Register("HeaderHorizontalAlignment", typeof(HorizontalAlignment), typeof(UGroupBox), new PropertyMetadata(HorizontalAlignment.Center));


    }
}

2、GroupBox 资源样式布局

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:MotionControls.Controls">

  <Style TargetType="{x:Type local:UGroupBox}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:UGroupBox}">

          <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
              x:Name="Header"
              Grid.Row="0"
              Grid.RowSpan="2"
              Grid.Column="2"
              HorizontalAlignment="{Binding HeaderHorizontalAlignment, RelativeSource={RelativeSource Mode=TemplatedParent}}"
              Panel.ZIndex="1"
              Background="{TemplateBinding Background}"
              BorderThickness="{TemplateBinding BorderThickness}">
              <ContentPresenter
                ContentSource="Header"
                RecognizesAccessKey="True"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            </Border>

            <Border
              Grid.Row="1"
              Grid.RowSpan="3"
              Grid.ColumnSpan="4"
              Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}"
              RenderTransformOrigin="0.5,0.5" />

            <ContentPresenter
              Grid.Row="2"
              Grid.Column="1"
              Grid.ColumnSpan="2"
              HorizontalAlignment="{Binding HeaderHorizontalAlignment, RelativeSource={RelativeSource Mode=TemplatedParent}}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
          </Grid>

        </ControlTemplate>

      </Setter.Value>
    </Setter>
  </Style>

</ResourceDictionary>

3、调用

<YOI:UGroupBox
  Grid.Row="0"
  Grid.Column="3"
  Width="290"
  Margin="10,10,0,0"
  HorizontalAlignment="Right"
  Background="{DynamicResource SecundaryBackgroundColor}"
  BorderBrush="{DynamicResource PrimaryGrayColor}"
  BorderThickness="0.1"
  HeaderHorizontalAlignmentProperty="Center">
  <YOI:UGroupBox.Header>
    <YOI:UButton
      Width="Auto"
      Height="24"
      Padding="5,0,5,0"
      Background="{DynamicResource PrimaryBlueColor}"
      BorderBrush="{DynamicResource PrimaryBlueColor}"
      Content="当前位置显示"
      FontSize="14"
      Foreground="White" />
  </YOI:UGroupBox.Header>

  <StackPanel
    Width="Auto"
    VerticalAlignment="Top"
    Orientation="Horizontal">
    <StackPanel Margin="10" Orientation="Horizontal">
      <Label
        Margin="0,0,5,0"
        Content="Z轴"
        Foreground="{DynamicResource TextPrimaryColor}" />
      <YOI:UTextBox
        Width="125"
        Height="30"
        HorizontalAlignment="Center"
        Background="White"
        BorderBrush="{DynamicResource PrimaryBlueColor}"
        ColseIconVisibility="Hidden"
        CornerRadius="15"
        FontSize="12"
        Placeholder="Z轴位置" />
      <Label
        Margin="5,0,0,0"
        Content="mm"
        Foreground="{DynamicResource TextPrimaryColor}" />
    </StackPanel>
  </StackPanel>
  
</YOI:UGroupBox>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值