回答編集履歴

1

見直しキャンペーン中

2023/07/29 08:11

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,153 +1,77 @@
1
1
  うまく説明できているか自信がないのですが、`Button`自体のプロパティと`Template`は直接は関連しません。
2
-
3
2
  連動して見えるのは連動するように作っているからで、すべて無視することもできてしまいます。
4
-
5
-
6
3
 
7
4
  現状`Content`と`ContentTemplate`だけが連動(`TemplateBinding`)している状態で、他は無視している形になっています。
8
5
 
9
-
10
-
11
6
  > ①ボタンをマウスオーバーした時に背景色が変わらない
12
-
13
-
14
7
 
15
8
  `Button`の`BorderBrush`・`Background`は変わっているが、それをだれも使っていない。
16
9
 
17
-
18
-
19
10
  > ②ボタンのContentsで指定した文字が左上揃いになってしまう
20
-
21
-
22
11
 
23
12
  `HorizontalContentAlignment`・`VerticalContentAlignment`を設定したが、それをだれも使っていない。
24
13
 
25
14
 
26
-
27
-
28
-
29
15
  素直に書くとこんな感じでしょうか。
30
-
31
- ```xaml
16
+ ```xml
32
-
33
17
  <Application.Resources>
34
-
35
18
  <!-- 色の指定 -->
36
-
37
19
  <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD" />
38
-
39
20
  <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070" />
40
-
41
21
  <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD" />
42
-
43
22
  <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1" />
44
-
45
23
  <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6" />
46
-
47
24
  <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B" />
48
-
49
25
  <!-- ボタン用共通スタイル -->
50
-
51
26
  <Style x:Key="CornerRadiusButton" TargetType="Button">
52
-
53
27
  <!-- Contentsの左右方向align -->
54
-
55
28
  <Setter Property="HorizontalContentAlignment" Value="Center" />
56
-
57
29
  <!-- Contentsの上下方向align -->
58
-
59
30
  <Setter Property="VerticalContentAlignment" Value="Center" />
60
-
61
31
  <!-- 角丸と背景色の設定 -->
62
-
63
32
  <Setter Property="Template">
64
-
65
33
  <Setter.Value>
66
-
67
34
  <ControlTemplate TargetType="Button">
68
-
69
35
  <!-- 角丸と通常背景色の設定 -->
70
-
71
36
  <Border
72
-
73
37
  Background="{TemplateBinding Background}"
74
-
75
38
  BorderBrush="{TemplateBinding BorderBrush}"
76
-
77
39
  BorderThickness="1"
78
-
79
40
  CornerRadius="6">
80
-
81
41
  <ContentPresenter
82
-
83
42
  x:Name="contentPresenter"
84
-
85
43
  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
86
-
87
44
  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
88
-
89
45
  Content="{TemplateBinding Content}"
90
-
91
46
  ContentTemplate="{TemplateBinding ContentTemplate}" />
92
-
93
47
  </Border>
94
-
95
48
  <!-- マウスオーバー時背景色の設定 -->
96
-
97
49
  <ControlTemplate.Triggers>
98
-
99
50
  <Trigger Property="IsMouseOver" Value="True">
100
-
101
51
  <Setter Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}" />
102
-
103
52
  <Setter Property="Background" Value="{StaticResource Button.MouseOver.Background}" />
104
-
105
53
  </Trigger>
106
-
107
54
  </ControlTemplate.Triggers>
108
-
109
55
  </ControlTemplate>
110
-
111
56
  </Setter.Value>
112
-
113
57
  </Setter>
114
-
115
58
  </Style>
116
-
117
59
  </Application.Resources>
118
-
119
60
  ```
120
-
121
61
  デザイナでコントロールを右クリックすると、「テンプレートの編集」ー「コピーして編集...」というメニューが出ます。
122
-
123
62
  それで出力したスタイルをベースにいじると楽です^^
124
-
125
-
126
63
 
127
64
  ---
128
65
 
129
-
130
-
131
66
  単に角丸にしたいだけならこれだけで十分です。
132
67
 
133
-
134
-
135
- ```xaml
68
+ ```xml
136
-
137
69
  <Style TargetType="Button" x:Key="CornerRadiusButton">
138
-
139
70
  <Style.Resources>
140
-
141
71
  <Style TargetType="Border">
142
-
143
72
  <Setter Property="CornerRadius" Value="6" />
144
-
145
73
  </Style>
146
-
147
74
  </Style.Resources>
148
-
149
75
  </Style>
150
-
151
76
  ```
152
-
153
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)