teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/29 08:11

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,77 +1,77 @@
1
- うまく説明できているか自信がないのですが、`Button`自体のプロパティと`Template`は直接は関連しません。
2
- 連動して見えるのは連動するように作っているからで、すべて無視することもできてしまいます。
3
-
4
- 現状`Content`と`ContentTemplate`だけが連動(`TemplateBinding`)している状態で、他は無視している形になっています。
5
-
6
- > ①ボタンをマウスオーバーした時に背景色が変わらない
7
-
8
- `Button`の`BorderBrush`・`Background`は変わっているが、それをだれも使っていない。
9
-
10
- > ②ボタンのContentsで指定した文字が左上揃いになってしまう
11
-
12
- `HorizontalContentAlignment`・`VerticalContentAlignment`を設定したが、それをだれも使っていない。
13
-
14
-
15
- 素直に書くとこんな感じでしょうか。
16
- ```xaml
17
- <Application.Resources>
18
- <!-- 色の指定 -->
19
- <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD" />
20
- <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070" />
21
- <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD" />
22
- <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1" />
23
- <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6" />
24
- <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B" />
25
- <!-- ボタン用共通スタイル -->
26
- <Style x:Key="CornerRadiusButton" TargetType="Button">
27
- <!-- Contentsの左右方向align -->
28
- <Setter Property="HorizontalContentAlignment" Value="Center" />
29
- <!-- Contentsの上下方向align -->
30
- <Setter Property="VerticalContentAlignment" Value="Center" />
31
- <!-- 角丸と背景色の設定 -->
32
- <Setter Property="Template">
33
- <Setter.Value>
34
- <ControlTemplate TargetType="Button">
35
- <!-- 角丸と通常背景色の設定 -->
36
- <Border
37
- Background="{TemplateBinding Background}"
38
- BorderBrush="{TemplateBinding BorderBrush}"
39
- BorderThickness="1"
40
- CornerRadius="6">
41
- <ContentPresenter
42
- x:Name="contentPresenter"
43
- HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
44
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
45
- Content="{TemplateBinding Content}"
46
- ContentTemplate="{TemplateBinding ContentTemplate}" />
47
- </Border>
48
- <!-- マウスオーバー時背景色の設定 -->
49
- <ControlTemplate.Triggers>
50
- <Trigger Property="IsMouseOver" Value="True">
51
- <Setter Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}" />
52
- <Setter Property="Background" Value="{StaticResource Button.MouseOver.Background}" />
53
- </Trigger>
54
- </ControlTemplate.Triggers>
55
- </ControlTemplate>
56
- </Setter.Value>
57
- </Setter>
58
- </Style>
59
- </Application.Resources>
60
- ```
61
- デザイナでコントロールを右クリックすると、「テンプレートの編集」ー「コピーして編集...」というメニューが出ます。
62
- それで出力したスタイルをベースにいじると楽です^^
63
-
64
- ---
65
-
66
- 単に角丸にしたいだけならこれだけで十分です。
67
-
68
- ```xaml
69
- <Style TargetType="Button" x:Key="CornerRadiusButton">
70
- <Style.Resources>
71
- <Style TargetType="Border">
72
- <Setter Property="CornerRadius" Value="6" />
73
- </Style>
74
- </Style.Resources>
75
- </Style>
76
- ```
1
+ うまく説明できているか自信がないのですが、`Button`自体のプロパティと`Template`は直接は関連しません。
2
+ 連動して見えるのは連動するように作っているからで、すべて無視することもできてしまいます。
3
+
4
+ 現状`Content`と`ContentTemplate`だけが連動(`TemplateBinding`)している状態で、他は無視している形になっています。
5
+
6
+ > ①ボタンをマウスオーバーした時に背景色が変わらない
7
+
8
+ `Button`の`BorderBrush`・`Background`は変わっているが、それをだれも使っていない。
9
+
10
+ > ②ボタンのContentsで指定した文字が左上揃いになってしまう
11
+
12
+ `HorizontalContentAlignment`・`VerticalContentAlignment`を設定したが、それをだれも使っていない。
13
+
14
+
15
+ 素直に書くとこんな感じでしょうか。
16
+ ```xml
17
+ <Application.Resources>
18
+ <!-- 色の指定 -->
19
+ <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD" />
20
+ <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070" />
21
+ <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD" />
22
+ <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1" />
23
+ <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6" />
24
+ <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B" />
25
+ <!-- ボタン用共通スタイル -->
26
+ <Style x:Key="CornerRadiusButton" TargetType="Button">
27
+ <!-- Contentsの左右方向align -->
28
+ <Setter Property="HorizontalContentAlignment" Value="Center" />
29
+ <!-- Contentsの上下方向align -->
30
+ <Setter Property="VerticalContentAlignment" Value="Center" />
31
+ <!-- 角丸と背景色の設定 -->
32
+ <Setter Property="Template">
33
+ <Setter.Value>
34
+ <ControlTemplate TargetType="Button">
35
+ <!-- 角丸と通常背景色の設定 -->
36
+ <Border
37
+ Background="{TemplateBinding Background}"
38
+ BorderBrush="{TemplateBinding BorderBrush}"
39
+ BorderThickness="1"
40
+ CornerRadius="6">
41
+ <ContentPresenter
42
+ x:Name="contentPresenter"
43
+ HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
44
+ VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
45
+ Content="{TemplateBinding Content}"
46
+ ContentTemplate="{TemplateBinding ContentTemplate}" />
47
+ </Border>
48
+ <!-- マウスオーバー時背景色の設定 -->
49
+ <ControlTemplate.Triggers>
50
+ <Trigger Property="IsMouseOver" Value="True">
51
+ <Setter Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}" />
52
+ <Setter Property="Background" Value="{StaticResource Button.MouseOver.Background}" />
53
+ </Trigger>
54
+ </ControlTemplate.Triggers>
55
+ </ControlTemplate>
56
+ </Setter.Value>
57
+ </Setter>
58
+ </Style>
59
+ </Application.Resources>
60
+ ```
61
+ デザイナでコントロールを右クリックすると、「テンプレートの編集」ー「コピーして編集...」というメニューが出ます。
62
+ それで出力したスタイルをベースにいじると楽です^^
63
+
64
+ ---
65
+
66
+ 単に角丸にしたいだけならこれだけで十分です。
67
+
68
+ ```xml
69
+ <Style TargetType="Button" x:Key="CornerRadiusButton">
70
+ <Style.Resources>
71
+ <Style TargetType="Border">
72
+ <Setter Property="CornerRadius" Value="6" />
73
+ </Style>
74
+ </Style.Resources>
75
+ </Style>
76
+ ```
77
77
  [.net - How to create/make rounded corner buttons in WPF? - Stack Overflow](https://stackoverflow.com/questions/6745663/how-to-create-make-rounded-corner-buttons-in-wpf)