回答編集履歴

3

見直しキャンペーン中

2023/07/26 13:37

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -1,235 +1,118 @@
1
1
  `VisualStateManager`か`Trigger`かで変えることになります。
2
2
 
3
-
4
-
5
3
  既定のテンプレートを見ると参考になります。
6
-
7
4
  通常の`Calendar`を置いてデザイナで右クリックし、「追加テンプレートの編集」-「CalendarDayButtonStyle の編集」-「コピーして編集」で既定のテンプレートが見れます。
8
-
9
5
  もしくは、[カレンダーのスタイルとテンプレート - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/controls/calendar-styles-and-templates?view=netframeworkdesktop-4.8)
10
6
 
11
7
 
12
-
13
-
14
-
15
8
  `VisualStateManager`はかなり冗長なので、アニメーションが不要な時は`Trigger`が手軽です。
16
-
17
9
  その際は使える依存関係プロパティがないかを確認します。
18
-
19
-
20
10
 
21
11
  [CalendarDayButton.IsSelectedProperty フィールド (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.calendardaybutton.isselectedproperty?view=netframework-4.7.2)
22
12
 
23
13
 
24
-
25
-
26
-
27
14
  提示コードだけでは動かせないので、`ItemsControl`は省略させていただきました。
28
15
 
29
-
30
-
31
- ```xaml
16
+ ```xml
32
-
33
17
  <Window
34
-
35
18
  x:Class="Questions319493.MainWindow"
36
-
37
19
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
-
39
20
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
40
-
41
21
  Width="1200"
42
-
43
22
  Height="600">
44
-
45
23
  <StackPanel Orientation="Horizontal">
46
24
 
47
-
48
-
49
25
  <!-- VisualStateManager -->
50
-
51
26
  <Calendar SelectedDate="{Binding SelectedDate}" SelectionMode="MultipleRange">
52
-
53
27
  <Calendar.Background>White</Calendar.Background>
54
-
55
28
  <Calendar.CalendarDayButtonStyle>
56
-
57
29
  <Style TargetType="{x:Type CalendarDayButton}">
58
-
59
30
  <Setter Property="Template">
60
-
61
31
  <Setter.Value>
62
-
63
32
  <ControlTemplate TargetType="{x:Type CalendarDayButton}">
64
-
65
33
  <Grid Background="White">
66
34
 
35
+ <!-- 色を付けるのはなんでもいいですが、既定の作りに合わせました -->
36
+ <Rectangle
37
+ x:Name="SelectedBackground"
38
+ Fill="#FFBADDE9"
39
+ Opacity="0"
40
+ RadiusX="1"
41
+ RadiusY="1" />
67
42
 
43
+ <Border BorderBrush="Turquoise" BorderThickness="1">
44
+ <StackPanel MinWidth="80" MinHeight="80">
45
+ <TextBlock
46
+ Margin="2"
47
+ FontSize="16"
48
+ Text="{Binding StringFormat={}{0:dd}}" />
49
+ </StackPanel>
50
+ </Border>
68
51
 
69
- <!-- 色を付けるのなんでいいですが、既定の作り合わせました -->
52
+ <!-- 本来はもっとステートがありますが選択ついてだけ -->
53
+ <VisualStateManager.VisualStateGroups>
54
+ <VisualStateGroup x:Name="SelectionStates">
55
+ <VisualStateGroup.Transitions>
56
+ <VisualTransition GeneratedDuration="0" />
57
+ </VisualStateGroup.Transitions>
58
+ <VisualState x:Name="Unselected" />
59
+ <VisualState x:Name="Selected">
60
+ <Storyboard>
61
+ <DoubleAnimation
62
+ Storyboard.TargetName="SelectedBackground"
63
+ Storyboard.TargetProperty="Opacity"
64
+ To=".75"
65
+ Duration="0" />
66
+ </Storyboard>
67
+ </VisualState>
68
+ </VisualStateGroup>
69
+ </VisualStateManager.VisualStateGroups>
70
+ </Grid>
71
+ </ControlTemplate>
72
+ </Setter.Value>
73
+ </Setter>
74
+ </Style>
75
+ </Calendar.CalendarDayButtonStyle>
76
+ </Calendar>
77
+
78
+ <!-- Trigger -->
79
+ <Calendar SelectedDate="{Binding SelectedDate}">
80
+ <Calendar.Background>White</Calendar.Background>
81
+ <Calendar.CalendarDayButtonStyle>
82
+ <Style TargetType="{x:Type CalendarDayButton}">
83
+ <Setter Property="Template">
84
+ <Setter.Value>
85
+ <ControlTemplate TargetType="{x:Type CalendarDayButton}">
86
+ <Grid Background="White">
70
87
 
71
88
  <Rectangle
72
-
73
89
  x:Name="SelectedBackground"
74
-
75
90
  Fill="#FFBADDE9"
76
-
77
91
  Opacity="0"
78
-
79
92
  RadiusX="1"
80
-
81
93
  RadiusY="1" />
82
94
 
83
-
84
-
85
95
  <Border BorderBrush="Turquoise" BorderThickness="1">
86
-
87
96
  <StackPanel MinWidth="80" MinHeight="80">
88
-
89
97
  <TextBlock
90
-
91
98
  Margin="2"
92
-
93
99
  FontSize="16"
94
-
95
100
  Text="{Binding StringFormat={}{0:dd}}" />
96
-
97
101
  </StackPanel>
98
-
99
102
  </Border>
100
-
101
-
102
-
103
- <!-- 本来はもっとステートがありますが選択についてだけ -->
104
-
105
- <VisualStateManager.VisualStateGroups>
106
-
107
- <VisualStateGroup x:Name="SelectionStates">
108
-
109
- <VisualStateGroup.Transitions>
110
-
111
- <VisualTransition GeneratedDuration="0" />
112
-
113
- </VisualStateGroup.Transitions>
114
-
115
- <VisualState x:Name="Unselected" />
116
-
117
- <VisualState x:Name="Selected">
118
-
119
- <Storyboard>
120
-
121
- <DoubleAnimation
122
-
123
- Storyboard.TargetName="SelectedBackground"
124
-
125
- Storyboard.TargetProperty="Opacity"
126
-
127
- To=".75"
128
-
129
- Duration="0" />
130
-
131
- </Storyboard>
132
-
133
- </VisualState>
134
-
135
- </VisualStateGroup>
136
-
137
- </VisualStateManager.VisualStateGroups>
138
-
139
103
  </Grid>
140
104
 
105
+ <ControlTemplate.Triggers>
106
+ <Trigger Property="IsSelected" Value="True">
107
+ <Setter TargetName="SelectedBackground" Property="Opacity" Value=".75" />
108
+ </Trigger>
109
+ </ControlTemplate.Triggers>
141
110
  </ControlTemplate>
142
-
143
111
  </Setter.Value>
144
-
145
112
  </Setter>
146
-
147
113
  </Style>
148
-
149
114
  </Calendar.CalendarDayButtonStyle>
150
-
151
115
  </Calendar>
152
-
153
-
154
-
155
- <!-- Trigger -->
156
-
157
- <Calendar SelectedDate="{Binding SelectedDate}">
158
-
159
- <Calendar.Background>White</Calendar.Background>
160
-
161
- <Calendar.CalendarDayButtonStyle>
162
-
163
- <Style TargetType="{x:Type CalendarDayButton}">
164
-
165
- <Setter Property="Template">
166
-
167
- <Setter.Value>
168
-
169
- <ControlTemplate TargetType="{x:Type CalendarDayButton}">
170
-
171
- <Grid Background="White">
172
-
173
-
174
-
175
- <Rectangle
176
-
177
- x:Name="SelectedBackground"
178
-
179
- Fill="#FFBADDE9"
180
-
181
- Opacity="0"
182
-
183
- RadiusX="1"
184
-
185
- RadiusY="1" />
186
-
187
-
188
-
189
- <Border BorderBrush="Turquoise" BorderThickness="1">
190
-
191
- <StackPanel MinWidth="80" MinHeight="80">
192
-
193
- <TextBlock
194
-
195
- Margin="2"
196
-
197
- FontSize="16"
198
-
199
- Text="{Binding StringFormat={}{0:dd}}" />
200
-
201
- </StackPanel>
202
-
203
- </Border>
204
-
205
- </Grid>
206
-
207
-
208
-
209
- <ControlTemplate.Triggers>
210
-
211
- <Trigger Property="IsSelected" Value="True">
212
-
213
- <Setter TargetName="SelectedBackground" Property="Opacity" Value=".75" />
214
-
215
- </Trigger>
216
-
217
- </ControlTemplate.Triggers>
218
-
219
- </ControlTemplate>
220
-
221
- </Setter.Value>
222
-
223
- </Setter>
224
-
225
- </Style>
226
-
227
- </Calendar.CalendarDayButtonStyle>
228
-
229
- </Calendar>
230
-
231
116
  </StackPanel>
232
-
233
117
  </Window>
234
-
235
118
  ```

2

無駄改行

2021/01/30 11:54

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -6,9 +6,7 @@
6
6
 
7
7
  通常の`Calendar`を置いてデザイナで右クリックし、「追加テンプレートの編集」-「CalendarDayButtonStyle の編集」-「コピーして編集」で既定のテンプレートが見れます。
8
8
 
9
- もしくは
10
-
11
- [カレンダーのスタイルとテンプレート - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/controls/calendar-styles-and-templates?view=netframeworkdesktop-4.8)
9
+ もしくは、[カレンダーのスタイルとテンプレート - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/controls/calendar-styles-and-templates?view=netframeworkdesktop-4.8)
12
10
 
13
11
 
14
12
 

1

無駄スペース

2021/01/30 11:54

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
 
28
28
 
29
- 提示コードだけでは動かせないので、`ItemsControl `は省略させていただきました。
29
+ 提示コードだけでは動かせないので、`ItemsControl`は省略させていただきました。
30
30
 
31
31
 
32
32