回答編集履歴
2
BasedOn
test
CHANGED
@@ -106,10 +106,16 @@
|
|
106
106
|
Height="600">
|
107
107
|
<Grid>
|
108
108
|
<Calendar
|
109
|
-
|
109
|
+
x:Name="calendar"
|
110
110
|
PreviewKeyDown="Calendar_PreviewKeyDown"
|
111
111
|
SelectedDatesChanged="Calendar_SelectedDatesChanged"
|
112
|
-
SelectionMode="MultipleRange"
|
112
|
+
SelectionMode="MultipleRange">
|
113
|
+
<Calendar.CalendarDayButtonStyle>
|
114
|
+
<Style BasedOn="{StaticResource CalendarDayButtonStyle1}" TargetType="CalendarDayButton">
|
115
|
+
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="CalendarDayButton_PreviewMouseLeftButtonDown" />
|
116
|
+
</Style>
|
117
|
+
</Calendar.CalendarDayButtonStyle>
|
118
|
+
</Calendar>
|
113
119
|
</Grid>
|
114
120
|
</Window>
|
115
121
|
```
|
@@ -118,6 +124,7 @@
|
|
118
124
|
using System.Diagnostics;
|
119
125
|
using System.Windows;
|
120
126
|
using System.Windows.Controls;
|
127
|
+
using System.Windows.Controls.Primitives;
|
121
128
|
using System.Windows.Input;
|
122
129
|
|
123
130
|
namespace Qil3fftqm79tcvt
|
@@ -136,6 +143,21 @@
|
|
136
143
|
if (0 < e.RemovedItems.Count)
|
137
144
|
Debug.WriteLine($"remove: {(DateTime)e.RemovedItems[0]:yyyy/MM/dd}");
|
138
145
|
}
|
146
|
+
|
147
|
+
private void CalendarDayButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
148
|
+
{
|
149
|
+
if (sender is CalendarDayButton dayButton && !dayButton.IsBlackedOut)
|
150
|
+
{
|
151
|
+
var date = (DateTime)dayButton.DataContext;
|
152
|
+
|
153
|
+
if (calendar.SelectedDates.Contains(date))
|
154
|
+
calendar.SelectedDates.Remove(date);
|
155
|
+
else
|
156
|
+
calendar.SelectedDates.Add(date);
|
157
|
+
|
158
|
+
e.Handled = true;
|
159
|
+
}
|
160
|
+
}
|
139
161
|
}
|
140
162
|
}
|
141
163
|
```
|
@@ -143,12 +165,10 @@
|
|
143
165
|
#### Dictionary1.xaml
|
144
166
|
```xml:Dictionary1.xaml
|
145
167
|
<ResourceDictionary
|
146
|
-
x:Class="Qil3fftqm79tcvt.Dictionary1"
|
147
168
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
148
169
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
149
170
|
|
150
171
|
<Style x:Key="CalendarDayButtonStyle1" TargetType="{x:Type CalendarDayButton}">
|
151
|
-
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="CalendarDayButton_PreviewMouseLeftButtonDown" />
|
152
172
|
<Setter Property="Template">
|
153
173
|
<Setter.Value>
|
154
174
|
<ControlTemplate TargetType="{x:Type CalendarDayButton}">
|
@@ -194,46 +214,3 @@
|
|
194
214
|
|
195
215
|
</ResourceDictionary>
|
196
216
|
```
|
197
|
-
```cs:Dictionary1.xaml.cs
|
198
|
-
using System;
|
199
|
-
using System.Windows;
|
200
|
-
using System.Windows.Controls;
|
201
|
-
using System.Windows.Controls.Primitives;
|
202
|
-
using System.Windows.Input;
|
203
|
-
using System.Windows.Media;
|
204
|
-
|
205
|
-
namespace Qil3fftqm79tcvt
|
206
|
-
{
|
207
|
-
public partial class Dictionary1 : ResourceDictionary
|
208
|
-
{
|
209
|
-
private void CalendarDayButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
210
|
-
{
|
211
|
-
if (sender is CalendarDayButton dayButton && !dayButton.IsBlackedOut)
|
212
|
-
{
|
213
|
-
var calendar = dayButton.FindAncestor<Calendar>();
|
214
|
-
var date = (DateTime)dayButton.DataContext;
|
215
|
-
|
216
|
-
if (calendar.SelectedDates.Contains(date))
|
217
|
-
calendar.SelectedDates.Remove(date);
|
218
|
-
else
|
219
|
-
calendar.SelectedDates.Add(date);
|
220
|
-
|
221
|
-
e.Handled = true;
|
222
|
-
}
|
223
|
-
}
|
224
|
-
}
|
225
|
-
|
226
|
-
public static class DependencyObjectExtensions
|
227
|
-
{
|
228
|
-
public static T FindAncestor<T>(this DependencyObject obj) where T : DependencyObject
|
229
|
-
{
|
230
|
-
while (obj != null)
|
231
|
-
{
|
232
|
-
if (obj is T target) return target;
|
233
|
-
obj = VisualTreeHelper.GetParent(obj);
|
234
|
-
}
|
235
|
-
return null;
|
236
|
-
}
|
237
|
-
}
|
238
|
-
}
|
239
|
-
```
|
1
追記 ControlTemplate & ResourceDictionary 版
test
CHANGED
@@ -90,4 +90,150 @@
|
|
90
90
|
}
|
91
91
|
```
|
92
92
|
|
93
|
-
`ControlTemplate`が不明なので`Style.Triggers`を使いましたが、`ControlTemplate.Triggers`でもいいです。
|
93
|
+
~~`ControlTemplate`が不明なので`Style.Triggers`を使いましたが、`ControlTemplate.Triggers`でもいいです。~~
|
94
|
+
|
95
|
+
---
|
96
|
+
|
97
|
+
## 追記 `ControlTemplate`&`ResourceDictionary`版
|
98
|
+
|
99
|
+
#### MainWindow.xaml
|
100
|
+
```xml:MainWindow.xaml
|
101
|
+
<Window
|
102
|
+
x:Class="Qil3fftqm79tcvt.MainWindow"
|
103
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
104
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
105
|
+
Width="800"
|
106
|
+
Height="600">
|
107
|
+
<Grid>
|
108
|
+
<Calendar
|
109
|
+
CalendarDayButtonStyle="{StaticResource CalendarDayButtonStyle1}"
|
110
|
+
PreviewKeyDown="Calendar_PreviewKeyDown"
|
111
|
+
SelectedDatesChanged="Calendar_SelectedDatesChanged"
|
112
|
+
SelectionMode="MultipleRange" />
|
113
|
+
</Grid>
|
114
|
+
</Window>
|
115
|
+
```
|
116
|
+
```cs:MainWindow.xaml.cs
|
117
|
+
using System;
|
118
|
+
using System.Diagnostics;
|
119
|
+
using System.Windows;
|
120
|
+
using System.Windows.Controls;
|
121
|
+
using System.Windows.Input;
|
122
|
+
|
123
|
+
namespace Qil3fftqm79tcvt
|
124
|
+
{
|
125
|
+
public partial class MainWindow : Window
|
126
|
+
{
|
127
|
+
public MainWindow() => InitializeComponent();
|
128
|
+
|
129
|
+
private void Calendar_PreviewKeyDown(object sender, KeyEventArgs e) => e.Handled = true;
|
130
|
+
|
131
|
+
private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
|
132
|
+
{
|
133
|
+
if (0 < e.AddedItems.Count)
|
134
|
+
Debug.WriteLine($"add: {(DateTime)e.AddedItems[0]:yyyy/MM/dd}");
|
135
|
+
|
136
|
+
if (0 < e.RemovedItems.Count)
|
137
|
+
Debug.WriteLine($"remove: {(DateTime)e.RemovedItems[0]:yyyy/MM/dd}");
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
```
|
142
|
+
|
143
|
+
#### Dictionary1.xaml
|
144
|
+
```xml:Dictionary1.xaml
|
145
|
+
<ResourceDictionary
|
146
|
+
x:Class="Qil3fftqm79tcvt.Dictionary1"
|
147
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
148
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
149
|
+
|
150
|
+
<Style x:Key="CalendarDayButtonStyle1" TargetType="{x:Type CalendarDayButton}">
|
151
|
+
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="CalendarDayButton_PreviewMouseLeftButtonDown" />
|
152
|
+
<Setter Property="Template">
|
153
|
+
<Setter.Value>
|
154
|
+
<ControlTemplate TargetType="{x:Type CalendarDayButton}">
|
155
|
+
<Grid x:Name="calDate" Background="White">
|
156
|
+
<Border BorderBrush="Turquoise" BorderThickness="1">
|
157
|
+
<StackPanel MinWidth="80" MinHeight="80">
|
158
|
+
<TextBlock
|
159
|
+
Margin="2"
|
160
|
+
FontSize="16"
|
161
|
+
Text="{Binding StringFormat={}{0:dd}}" />
|
162
|
+
<ItemsControl>
|
163
|
+
<ItemsControl.ItemsPanel>
|
164
|
+
<ItemsPanelTemplate>
|
165
|
+
<WrapPanel Margin="2" />
|
166
|
+
</ItemsPanelTemplate>
|
167
|
+
</ItemsControl.ItemsPanel>
|
168
|
+
</ItemsControl>
|
169
|
+
</StackPanel>
|
170
|
+
</Border>
|
171
|
+
<Path
|
172
|
+
x:Name="Blackout"
|
173
|
+
Margin="3"
|
174
|
+
HorizontalAlignment="Stretch"
|
175
|
+
VerticalAlignment="Stretch"
|
176
|
+
Data="M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"
|
177
|
+
Fill="#FF000000"
|
178
|
+
Opacity="0"
|
179
|
+
RenderTransformOrigin="0.5,0.5"
|
180
|
+
Stretch="Fill" />
|
181
|
+
</Grid>
|
182
|
+
<ControlTemplate.Triggers>
|
183
|
+
<Trigger Property="IsSelected" Value="True">
|
184
|
+
<Setter TargetName="calDate" Property="Background" Value="#EAAA21" />
|
185
|
+
</Trigger>
|
186
|
+
<Trigger Property="IsBlackedOut" Value="True">
|
187
|
+
<Setter TargetName="Blackout" Property="Opacity" Value="0.2" />
|
188
|
+
</Trigger>
|
189
|
+
</ControlTemplate.Triggers>
|
190
|
+
</ControlTemplate>
|
191
|
+
</Setter.Value>
|
192
|
+
</Setter>
|
193
|
+
</Style>
|
194
|
+
|
195
|
+
</ResourceDictionary>
|
196
|
+
```
|
197
|
+
```cs:Dictionary1.xaml.cs
|
198
|
+
using System;
|
199
|
+
using System.Windows;
|
200
|
+
using System.Windows.Controls;
|
201
|
+
using System.Windows.Controls.Primitives;
|
202
|
+
using System.Windows.Input;
|
203
|
+
using System.Windows.Media;
|
204
|
+
|
205
|
+
namespace Qil3fftqm79tcvt
|
206
|
+
{
|
207
|
+
public partial class Dictionary1 : ResourceDictionary
|
208
|
+
{
|
209
|
+
private void CalendarDayButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
210
|
+
{
|
211
|
+
if (sender is CalendarDayButton dayButton && !dayButton.IsBlackedOut)
|
212
|
+
{
|
213
|
+
var calendar = dayButton.FindAncestor<Calendar>();
|
214
|
+
var date = (DateTime)dayButton.DataContext;
|
215
|
+
|
216
|
+
if (calendar.SelectedDates.Contains(date))
|
217
|
+
calendar.SelectedDates.Remove(date);
|
218
|
+
else
|
219
|
+
calendar.SelectedDates.Add(date);
|
220
|
+
|
221
|
+
e.Handled = true;
|
222
|
+
}
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
226
|
+
public static class DependencyObjectExtensions
|
227
|
+
{
|
228
|
+
public static T FindAncestor<T>(this DependencyObject obj) where T : DependencyObject
|
229
|
+
{
|
230
|
+
while (obj != null)
|
231
|
+
{
|
232
|
+
if (obj is T target) return target;
|
233
|
+
obj = VisualTreeHelper.GetParent(obj);
|
234
|
+
}
|
235
|
+
return null;
|
236
|
+
}
|
237
|
+
}
|
238
|
+
}
|
239
|
+
```
|