Skip to content

Commit 97110e9

Browse files
committed
feat: add avalonia HeaderedContentControl style.
1 parent 72abacf commit 97110e9

File tree

5 files changed

+155
-13
lines changed

5 files changed

+155
-13
lines changed

src/Avalonia/HandyControl_Avalonia/Controls/Attach/InfoElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void SetMinContentHeight(AvaloniaObject element, double value) =>
3838

3939
public static readonly AttachedProperty<double> MaxContentHeightProperty =
4040
AvaloniaProperty.RegisterAttached<InfoElement, AvaloniaObject, double>("MaxContentHeight",
41-
defaultValue: double.PositiveInfinity);
41+
defaultValue: double.PositiveInfinity, inherits: true);
4242

4343
public static void SetMaxContentHeight(AvaloniaObject element, double value) =>
4444
element.SetValue(MaxContentHeightProperty, value);

src/Avalonia/HandyControl_Avalonia/Controls/Attach/TitleElement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ public static void SetMarginOnTheTop(AvaloniaObject element, Thickness value) =>
9595
public static Thickness GetMarginOnTheTop(AvaloniaObject element) => element.GetValue(MarginOnTheTopProperty);
9696

9797
public static readonly AttachedProperty<double> MinHeightProperty =
98-
AvaloniaProperty.RegisterAttached<TitleElement, AvaloniaObject, double>("MinHeight");
98+
AvaloniaProperty.RegisterAttached<TitleElement, AvaloniaObject, double>("MinHeight", inherits: true);
9999

100100
public static void SetMinHeight(AvaloniaObject element, double value) => element.SetValue(MinHeightProperty, value);
101101

102102
public static double GetMinHeight(AvaloniaObject element) => element.GetValue(MinHeightProperty);
103103

104104
public static readonly AttachedProperty<double> MinWidthProperty =
105-
AvaloniaProperty.RegisterAttached<TitleElement, AvaloniaObject, double>("MinWidth");
105+
AvaloniaProperty.RegisterAttached<TitleElement, AvaloniaObject, double>("MinWidth", inherits: true);
106106

107107
public static void SetMinWidth(AvaloniaObject element, double value) => element.SetValue(MinWidthProperty, value);
108108

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<ResourceDictionary xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
x:ClassModifier="internal"
4+
xmlns:hc="clr-namespace:HandyControl.Controls"
5+
xmlns:sys="using:System">
6+
7+
<ControlTemplate x:Key="HeaderedContentControlTopTemplate"
8+
TargetType="HeaderedContentControl">
9+
<Border BorderBrush="{TemplateBinding BorderBrush}"
10+
BorderThickness="{TemplateBinding BorderThickness}"
11+
CornerRadius="{TemplateBinding CornerRadius}"
12+
Background="{TemplateBinding Background}"
13+
Padding="{TemplateBinding Padding}">
14+
<Grid>
15+
<Grid.RowDefinitions>
16+
<RowDefinition Height="Auto" />
17+
<RowDefinition MinHeight="{Binding Path=(hc:InfoElement.MinContentHeight), RelativeSource={RelativeSource TemplatedParent}}"
18+
Height="{Binding Path=(hc:InfoElement.ContentHeight), RelativeSource={RelativeSource TemplatedParent},Converter={StaticResource Double2GridLengthConverter}}" />
19+
</Grid.RowDefinitions>
20+
<DockPanel LastChildFill="True"
21+
IsHitTestVisible="False"
22+
IsVisible="{TemplateBinding Header, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
23+
HorizontalAlignment="{Binding Path=(hc:TitleElement.HorizontalAlignment), RelativeSource={RelativeSource TemplatedParent}}"
24+
Margin="{Binding Path=(hc:TitleElement.MarginOnTheTop), RelativeSource={RelativeSource TemplatedParent}}">
25+
<ContentPresenter DockPanel.Dock="Right"
26+
TextElement.Foreground="{DynamicResource DangerBrush}"
27+
Margin="4,0,0,0"
28+
Content="{Binding Path=(hc:InfoElement.Symbol), RelativeSource={RelativeSource TemplatedParent}}"
29+
IsVisible="{Binding Path=(hc:InfoElement.Necessary), RelativeSource={RelativeSource TemplatedParent}}" />
30+
<ContentPresenter Name="PART_HeaderPresenter"
31+
Content="{TemplateBinding Header}">
32+
<ContentPresenter.DataTemplates>
33+
<DataTemplate DataType="sys:String">
34+
<TextBlock Text="{Binding}"
35+
TextWrapping="NoWrap"
36+
TextTrimming="CharacterEllipsis"
37+
FontSize="{TemplateBinding FontSize}"
38+
FontWeight="{TemplateBinding FontWeight}"
39+
FontFamily="{TemplateBinding FontFamily}"
40+
FontStyle="{TemplateBinding FontStyle}" />
41+
</DataTemplate>
42+
</ContentPresenter.DataTemplates>
43+
</ContentPresenter>
44+
</DockPanel>
45+
<DataValidationErrors Grid.Row="1">
46+
<ContentPresenter Name="PART_ContentPresenter"
47+
ContentTemplate="{TemplateBinding ContentTemplate}"
48+
Content="{TemplateBinding Content}"
49+
RecognizesAccessKey="True"
50+
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
51+
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
52+
FontSize="{TemplateBinding FontSize}"
53+
FontWeight="{TemplateBinding FontWeight}"
54+
FontFamily="{TemplateBinding FontFamily}"
55+
FontStyle="{TemplateBinding FontStyle}" />
56+
</DataValidationErrors>
57+
</Grid>
58+
</Border>
59+
</ControlTemplate>
60+
61+
<ControlTemplate x:Key="HeaderedContentControlLeftTemplate"
62+
TargetType="HeaderedContentControl">
63+
<Border BorderBrush="{TemplateBinding BorderBrush}"
64+
BorderThickness="{TemplateBinding BorderThickness}"
65+
CornerRadius="{TemplateBinding CornerRadius}"
66+
Background="{TemplateBinding Background}"
67+
Padding="{TemplateBinding Padding}">
68+
<Grid MinHeight="{Binding Path=(hc:InfoElement.MinContentHeight), RelativeSource={RelativeSource TemplatedParent}}"
69+
Height="{Binding Path=(hc:InfoElement.ContentHeight), RelativeSource={RelativeSource TemplatedParent}}">
70+
<Grid.ColumnDefinitions>
71+
<ColumnDefinition Width="{Binding Path=(hc:TitleElement.TitleWidth), RelativeSource={RelativeSource TemplatedParent}}" />
72+
<ColumnDefinition />
73+
</Grid.ColumnDefinitions>
74+
<DockPanel LastChildFill="True"
75+
IsHitTestVisible="False"
76+
IsVisible="{TemplateBinding Header, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
77+
HorizontalAlignment="{Binding Path=(hc:TitleElement.HorizontalAlignment), RelativeSource={RelativeSource TemplatedParent}}"
78+
VerticalAlignment="{Binding Path=(hc:TitleElement.VerticalAlignment), RelativeSource={RelativeSource TemplatedParent}}"
79+
Margin="{Binding Path=(hc:TitleElement.MarginOnTheLeft), RelativeSource={RelativeSource TemplatedParent}}">
80+
<ContentPresenter DockPanel.Dock="Right"
81+
TextElement.Foreground="{DynamicResource DangerBrush}"
82+
Margin="4,0,0,0"
83+
Content="{Binding Path=(hc:InfoElement.Symbol), RelativeSource={RelativeSource TemplatedParent}}"
84+
IsVisible="{Binding Path=(hc:InfoElement.Necessary), RelativeSource={RelativeSource TemplatedParent}}" />
85+
<ContentPresenter Name="PART_HeaderPresenter"
86+
Content="{TemplateBinding Header}">
87+
<ContentPresenter.DataTemplates>
88+
<DataTemplate DataType="sys:String">
89+
<TextBlock Text="{Binding}"
90+
TextWrapping="NoWrap"
91+
TextTrimming="CharacterEllipsis"
92+
FontSize="{TemplateBinding FontSize}"
93+
FontWeight="{TemplateBinding FontWeight}"
94+
FontFamily="{TemplateBinding FontFamily}"
95+
FontStyle="{TemplateBinding FontStyle}" />
96+
</DataTemplate>
97+
</ContentPresenter.DataTemplates>
98+
</ContentPresenter>
99+
</DockPanel>
100+
<DataValidationErrors Grid.Column="1">
101+
<ContentPresenter Name="PART_ContentPresenter"
102+
ContentTemplate="{TemplateBinding ContentTemplate}"
103+
Content="{TemplateBinding Content}"
104+
RecognizesAccessKey="True"
105+
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
106+
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
107+
FontSize="{TemplateBinding FontSize}"
108+
FontWeight="{TemplateBinding FontWeight}"
109+
FontFamily="{TemplateBinding FontFamily}"
110+
FontStyle="{TemplateBinding FontStyle}" />
111+
</DataValidationErrors>
112+
</Grid>
113+
</Border>
114+
</ControlTemplate>
115+
116+
<ControlTheme x:Key="HeaderedContentControlBaseStyle"
117+
TargetType="HeaderedContentControl">
118+
<Setter Property="Template"
119+
Value="{StaticResource HeaderedContentControlTopTemplate}" />
120+
121+
<Style Selector="^[(hc|TitleElement.TitlePlacement)=Left]">
122+
<Setter Property="Template"
123+
Value="{StaticResource HeaderedContentControlLeftTemplate}" />
124+
</Style>
125+
</ControlTheme>
126+
127+
<ControlTheme x:Key="{x:Type HeaderedContentControl}"
128+
BasedOn="{StaticResource HeaderedContentControlBaseStyle}"
129+
TargetType="HeaderedContentControl" />
130+
131+
</ResourceDictionary>

src/Avalonia/HandyControl_Avalonia/Themes/Styles/MergedStyles.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Menu.axaml" />
5555
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/ContextMenu.axaml" />
5656
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/SelectableTextBlock.axaml" />
57+
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/HeaderedContentControl.axaml" />
5758
</ResourceDictionary.MergedDictionaries>
5859
</ResourceDictionary>
5960
</Styles.Resources>

src/Shared/HandyControl_Shared/Controls/Attach/InfoElement.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,32 @@ public class InfoElement : TitleElement
99
/// 占位符
1010
/// </summary>
1111
public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.RegisterAttached(
12-
"Placeholder", typeof(string), typeof(InfoElement), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
12+
"Placeholder", typeof(string), typeof(InfoElement),
13+
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
1314

14-
public static void SetPlaceholder(DependencyObject element, string value) => element.SetValue(PlaceholderProperty, value);
15+
public static void SetPlaceholder(DependencyObject element, string value) =>
16+
element.SetValue(PlaceholderProperty, value);
1517

1618
public static string GetPlaceholder(DependencyObject element) => (string) element.GetValue(PlaceholderProperty);
1719

1820
/// <summary>
1921
/// 是否必填
2022
/// </summary>
2123
public static readonly DependencyProperty NecessaryProperty = DependencyProperty.RegisterAttached(
22-
"Necessary", typeof(bool), typeof(InfoElement), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
24+
"Necessary", typeof(bool), typeof(InfoElement),
25+
new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
2326

24-
public static void SetNecessary(DependencyObject element, bool value) => element.SetValue(NecessaryProperty, ValueBoxes.BooleanBox(value));
27+
public static void SetNecessary(DependencyObject element, bool value) =>
28+
element.SetValue(NecessaryProperty, ValueBoxes.BooleanBox(value));
2529

2630
public static bool GetNecessary(DependencyObject element) => (bool) element.GetValue(NecessaryProperty);
2731

2832
/// <summary>
2933
/// 标记
3034
/// </summary>
3135
public static readonly DependencyProperty SymbolProperty = DependencyProperty.RegisterAttached(
32-
"Symbol", typeof(string), typeof(InfoElement), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
36+
"Symbol", typeof(string), typeof(InfoElement),
37+
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
3338

3439
public static void SetSymbol(DependencyObject element, string value) => element.SetValue(SymbolProperty, value);
3540

@@ -39,17 +44,20 @@ public class InfoElement : TitleElement
3944
/// 内容高度
4045
/// </summary>
4146
public static readonly DependencyProperty ContentHeightProperty = DependencyProperty.RegisterAttached(
42-
"ContentHeight", typeof(double), typeof(InfoElement), new FrameworkPropertyMetadata(28.0, FrameworkPropertyMetadataOptions.Inherits));
47+
"ContentHeight", typeof(double), typeof(InfoElement),
48+
new FrameworkPropertyMetadata(28.0, FrameworkPropertyMetadataOptions.Inherits));
4349

44-
public static void SetContentHeight(DependencyObject element, double value) => element.SetValue(ContentHeightProperty, value);
50+
public static void SetContentHeight(DependencyObject element, double value) =>
51+
element.SetValue(ContentHeightProperty, value);
4552

4653
public static double GetContentHeight(DependencyObject element) => (double) element.GetValue(ContentHeightProperty);
4754

4855
/// <summary>
4956
/// 最小内容高度
5057
/// </summary>
5158
public static readonly DependencyProperty MinContentHeightProperty = DependencyProperty.RegisterAttached(
52-
"MinContentHeight", typeof(double), typeof(InfoElement), new PropertyMetadata(28.0));
59+
"MinContentHeight", typeof(double), typeof(InfoElement),
60+
new FrameworkPropertyMetadata(28.0, FrameworkPropertyMetadataOptions.Inherits));
5361

5462
public static void SetMinContentHeight(DependencyObject element, double value)
5563
=> element.SetValue(MinContentHeightProperty, value);
@@ -61,7 +69,8 @@ public static double GetMinContentHeight(DependencyObject element)
6169
/// 最大内容高度
6270
/// </summary>
6371
public static readonly DependencyProperty MaxContentHeightProperty = DependencyProperty.RegisterAttached(
64-
"MaxContentHeight", typeof(double), typeof(InfoElement), new PropertyMetadata(double.PositiveInfinity));
72+
"MaxContentHeight", typeof(double), typeof(InfoElement),
73+
new FrameworkPropertyMetadata(double.PositiveInfinity, FrameworkPropertyMetadataOptions.Inherits));
6574

6675
public static void SetMaxContentHeight(DependencyObject element, double value)
6776
=> element.SetValue(MaxContentHeightProperty, value);
@@ -93,7 +102,8 @@ public static bool GetShowClearButton(DependencyObject element)
93102
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.RegisterAttached(
94103
"IsReadOnly", typeof(bool), typeof(InfoElement), new PropertyMetadata(ValueBoxes.FalseBox));
95104

96-
public static void SetIsReadOnly(DependencyObject element, bool value) => element.SetValue(IsReadOnlyProperty, ValueBoxes.BooleanBox(value));
105+
public static void SetIsReadOnly(DependencyObject element, bool value) =>
106+
element.SetValue(IsReadOnlyProperty, ValueBoxes.BooleanBox(value));
97107

98108
public static bool GetIsReadOnly(DependencyObject element) => (bool) element.GetValue(IsReadOnlyProperty);
99109
}

0 commit comments

Comments
 (0)