实现效果如图:
实现思路:为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>
评论记录:
回复评论: