回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,223 +1,223 @@
|
|
1
|
-
文字数上限で追記出来なかったため別回答にさせてください。
|
2
|
-
|
3
|
-
`ItemsControl`を使った例
|
4
|
-
|
5
|
-
```
|
6
|
-
<Window
|
7
|
-
x:Class="Questions259221.MainWindow"
|
8
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
9
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
10
|
-
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
11
|
-
SizeToContent="WidthAndHeight">
|
12
|
-
<StackPanel>
|
13
|
-
<StackPanel Orientation="Horizontal">
|
14
|
-
<ComboBox
|
15
|
-
x:Name="ChangeYEAR"
|
16
|
-
Width="100"
|
17
|
-
SelectionChanged="SelectionChanged" />
|
18
|
-
<ComboBox
|
19
|
-
x:Name="ChangeMONTH"
|
20
|
-
Width="100"
|
21
|
-
SelectionChanged="SelectionChanged" />
|
22
|
-
<Button
|
23
|
-
x:Name="Today"
|
24
|
-
Click="Today_Click"
|
25
|
-
Content="Today" />
|
26
|
-
</StackPanel>
|
27
|
-
|
28
|
-
<StackPanel Orientation="Horizontal">
|
29
|
-
|
30
|
-
<!-- 選択したい場合 -->
|
31
|
-
<ListBox
|
32
|
-
x:Name="calendarSelector"
|
33
|
-
Width="300"
|
34
|
-
AlternationCount="7">
|
35
|
-
<ItemsControl.Template>
|
36
|
-
<ControlTemplate TargetType="ItemsControl">
|
37
|
-
<ControlTemplate.Resources>
|
38
|
-
<Style TargetType="{x:Type TextBlock}">
|
39
|
-
<Setter Property="VerticalAlignment" Value="Center" />
|
40
|
-
<Setter Property="HorizontalAlignment" Value="Center" />
|
41
|
-
</Style>
|
42
|
-
</ControlTemplate.Resources>
|
43
|
-
<Border
|
44
|
-
Background="Azure"
|
45
|
-
BorderBrush="Black"
|
46
|
-
BorderThickness="1">
|
47
|
-
<StackPanel>
|
48
|
-
<UniformGrid Margin="10,10,10,0" Columns="7">
|
49
|
-
<TextBlock Foreground="Red" Text="Sun" />
|
50
|
-
<TextBlock Text="Mon" />
|
51
|
-
<TextBlock Text="Tue" />
|
52
|
-
<TextBlock Text="Wed" />
|
53
|
-
<TextBlock Text="Thu" />
|
54
|
-
<TextBlock Text="Fri" />
|
55
|
-
<TextBlock Foreground="Blue" Text="Sat" />
|
56
|
-
</UniformGrid>
|
57
|
-
<ItemsPresenter Margin="10" />
|
58
|
-
</StackPanel>
|
59
|
-
</Border>
|
60
|
-
</ControlTemplate>
|
61
|
-
</ItemsControl.Template>
|
62
|
-
<ItemsControl.ItemsPanel>
|
63
|
-
<ItemsPanelTemplate>
|
64
|
-
<UniformGrid Columns="7" />
|
65
|
-
</ItemsPanelTemplate>
|
66
|
-
</ItemsControl.ItemsPanel>
|
67
|
-
<ItemsControl.ItemTemplate>
|
68
|
-
<DataTemplate>
|
69
|
-
<Label
|
70
|
-
HorizontalContentAlignment="Center"
|
71
|
-
VerticalContentAlignment="Center"
|
72
|
-
Content="{Binding Day}"
|
73
|
-
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" />
|
74
|
-
</DataTemplate>
|
75
|
-
</ItemsControl.ItemTemplate>
|
76
|
-
<ItemsControl.ItemContainerStyle>
|
77
|
-
<Style TargetType="ListBoxItem">
|
78
|
-
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
|
79
|
-
<Setter Property="VerticalContentAlignment" Value="Stretch" />
|
80
|
-
<Style.Triggers>
|
81
|
-
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
|
82
|
-
<Setter Property="Foreground" Value="Red" />
|
83
|
-
</Trigger>
|
84
|
-
<Trigger Property="ItemsControl.AlternationIndex" Value="6">
|
85
|
-
<Setter Property="Foreground" Value="Blue" />
|
86
|
-
</Trigger>
|
87
|
-
<DataTrigger Binding="{Binding}" Value="{x:Static sys:DateTime.MinValue}">
|
88
|
-
<Setter Property="Visibility" Value="Hidden" />
|
89
|
-
</DataTrigger>
|
90
|
-
</Style.Triggers>
|
91
|
-
</Style>
|
92
|
-
</ItemsControl.ItemContainerStyle>
|
93
|
-
</ListBox>
|
94
|
-
|
95
|
-
<!-- 表示だけの場合 -->
|
96
|
-
<ItemsControl
|
97
|
-
x:Name="calendarView"
|
98
|
-
Width="300"
|
99
|
-
AlternationCount="7">
|
100
|
-
<ItemsControl.Template>
|
101
|
-
<ControlTemplate TargetType="ItemsControl">
|
102
|
-
<ControlTemplate.Resources>
|
103
|
-
<Style TargetType="{x:Type TextBlock}">
|
104
|
-
<Setter Property="VerticalAlignment" Value="Center" />
|
105
|
-
<Setter Property="HorizontalAlignment" Value="Center" />
|
106
|
-
</Style>
|
107
|
-
</ControlTemplate.Resources>
|
108
|
-
<Border
|
109
|
-
Background="Azure"
|
110
|
-
BorderBrush="Black"
|
111
|
-
BorderThickness="1">
|
112
|
-
<StackPanel>
|
113
|
-
<UniformGrid Margin="10,10,10,0" Columns="7">
|
114
|
-
<TextBlock Foreground="Red" Text="Sun" />
|
115
|
-
<TextBlock Text="Mon" />
|
116
|
-
<TextBlock Text="Tue" />
|
117
|
-
<TextBlock Text="Wed" />
|
118
|
-
<TextBlock Text="Thu" />
|
119
|
-
<TextBlock Text="Fri" />
|
120
|
-
<TextBlock Foreground="Blue" Text="Sat" />
|
121
|
-
</UniformGrid>
|
122
|
-
<ItemsPresenter Margin="10" />
|
123
|
-
</StackPanel>
|
124
|
-
</Border>
|
125
|
-
</ControlTemplate>
|
126
|
-
</ItemsControl.Template>
|
127
|
-
<ItemsControl.ItemsPanel>
|
128
|
-
<ItemsPanelTemplate>
|
129
|
-
<UniformGrid Columns="7" />
|
130
|
-
</ItemsPanelTemplate>
|
131
|
-
</ItemsControl.ItemsPanel>
|
132
|
-
<ItemsControl.ItemTemplate>
|
133
|
-
<DataTemplate>
|
134
|
-
<Label
|
135
|
-
HorizontalContentAlignment="Center"
|
136
|
-
VerticalContentAlignment="Center"
|
137
|
-
Content="{Binding Day}"
|
138
|
-
Foreground="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContentPresenter}}" />
|
139
|
-
</DataTemplate>
|
140
|
-
</ItemsControl.ItemTemplate>
|
141
|
-
<ItemsControl.ItemContainerStyle>
|
142
|
-
<Style TargetType="ContentPresenter">
|
143
|
-
<Setter Property="Tag" Value="Black" />
|
144
|
-
<Style.Triggers>
|
145
|
-
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
|
146
|
-
<Setter Property="Tag" Value="Red" />
|
147
|
-
</Trigger>
|
148
|
-
<Trigger Property="ItemsControl.AlternationIndex" Value="6">
|
149
|
-
<Setter Property="Tag" Value="Blue" />
|
150
|
-
</Trigger>
|
151
|
-
<DataTrigger Binding="{Binding}" Value="{x:Static sys:DateTime.MinValue}">
|
152
|
-
<Setter Property="Visibility" Value="Hidden" />
|
153
|
-
</DataTrigger>
|
154
|
-
</Style.Triggers>
|
155
|
-
</Style>
|
156
|
-
</ItemsControl.ItemContainerStyle>
|
157
|
-
</ItemsControl>
|
158
|
-
|
159
|
-
<!-- カレンダーを出したいだけなら -->
|
160
|
-
<Viewbox Width="300">
|
161
|
-
<Calendar x:Name="calendar" IsHitTestVisible="False" />
|
162
|
-
</Viewbox>
|
163
|
-
</StackPanel>
|
164
|
-
|
165
|
-
<TextBlock Text="{Binding SelectedItem, ElementName=calendarSelector, StringFormat=yyyy年MM月dd日}" />
|
166
|
-
|
167
|
-
</StackPanel>
|
168
|
-
</Window>
|
169
|
-
```
|
170
|
-
|
171
|
-
```
|
172
|
-
using System;
|
173
|
-
using System.Linq;
|
174
|
-
using System.Windows;
|
175
|
-
using System.Windows.Controls;
|
176
|
-
|
177
|
-
namespace Questions259221
|
178
|
-
{
|
179
|
-
public partial class MainWindow : Window
|
180
|
-
{
|
181
|
-
public MainWindow()
|
182
|
-
{
|
183
|
-
InitializeComponent();
|
184
|
-
|
185
|
-
var now = DateTime.Now;
|
186
|
-
ChangeYEAR.ItemsSource = Enumerable.Range(now.Year - 10, 21);
|
187
|
-
ChangeYEAR.SelectedItem = now.Year;
|
188
|
-
|
189
|
-
ChangeMONTH.ItemsSource = Enumerable.Range(1, 12);
|
190
|
-
ChangeMONTH.SelectedItem = now.Month;
|
191
|
-
}
|
192
|
-
|
193
|
-
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
|
194
|
-
{
|
195
|
-
if(ChangeYEAR.SelectedItem is int year && ChangeMONTH.SelectedItem is int month)
|
196
|
-
{
|
197
|
-
var firstDate = new DateTime(year, month, 1);
|
198
|
-
var lastDay = firstDate.AddMonths(1).AddDays(-1).Day;
|
199
|
-
var dayOfWeek = (int)firstDate.DayOfWeek;
|
200
|
-
|
201
|
-
var spacer = Enumerable.Repeat(DateTime.MinValue, dayOfWeek);
|
202
|
-
var days = Enumerable.Range(0, lastDay).Select(x => firstDate.AddDays(x));
|
203
|
-
var items = spacer.Concat(days);
|
204
|
-
calendarSelector.ItemsSource = items;
|
205
|
-
calendarView.ItemsSource = items;
|
206
|
-
calendar.DisplayDate = firstDate;
|
207
|
-
}
|
208
|
-
}
|
209
|
-
|
210
|
-
private void Today_Click(object sender, RoutedEventArgs e)
|
211
|
-
{
|
212
|
-
var now = DateTime.Now;
|
213
|
-
ChangeYEAR.SelectedItem = now.Year;
|
214
|
-
ChangeMONTH.SelectedItem = now.Month;
|
215
|
-
calendarSelector.SelectedItem = now.Date;
|
216
|
-
calendar.DisplayDate = now.Date;
|
217
|
-
}
|
218
|
-
}
|
219
|
-
}
|
220
|
-
```
|
221
|
-

|
222
|
-
|
1
|
+
文字数上限で追記出来なかったため別回答にさせてください。
|
2
|
+
|
3
|
+
`ItemsControl`を使った例
|
4
|
+
|
5
|
+
```xml
|
6
|
+
<Window
|
7
|
+
x:Class="Questions259221.MainWindow"
|
8
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
9
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
10
|
+
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
11
|
+
SizeToContent="WidthAndHeight">
|
12
|
+
<StackPanel>
|
13
|
+
<StackPanel Orientation="Horizontal">
|
14
|
+
<ComboBox
|
15
|
+
x:Name="ChangeYEAR"
|
16
|
+
Width="100"
|
17
|
+
SelectionChanged="SelectionChanged" />
|
18
|
+
<ComboBox
|
19
|
+
x:Name="ChangeMONTH"
|
20
|
+
Width="100"
|
21
|
+
SelectionChanged="SelectionChanged" />
|
22
|
+
<Button
|
23
|
+
x:Name="Today"
|
24
|
+
Click="Today_Click"
|
25
|
+
Content="Today" />
|
26
|
+
</StackPanel>
|
27
|
+
|
28
|
+
<StackPanel Orientation="Horizontal">
|
29
|
+
|
30
|
+
<!-- 選択したい場合 -->
|
31
|
+
<ListBox
|
32
|
+
x:Name="calendarSelector"
|
33
|
+
Width="300"
|
34
|
+
AlternationCount="7">
|
35
|
+
<ItemsControl.Template>
|
36
|
+
<ControlTemplate TargetType="ItemsControl">
|
37
|
+
<ControlTemplate.Resources>
|
38
|
+
<Style TargetType="{x:Type TextBlock}">
|
39
|
+
<Setter Property="VerticalAlignment" Value="Center" />
|
40
|
+
<Setter Property="HorizontalAlignment" Value="Center" />
|
41
|
+
</Style>
|
42
|
+
</ControlTemplate.Resources>
|
43
|
+
<Border
|
44
|
+
Background="Azure"
|
45
|
+
BorderBrush="Black"
|
46
|
+
BorderThickness="1">
|
47
|
+
<StackPanel>
|
48
|
+
<UniformGrid Margin="10,10,10,0" Columns="7">
|
49
|
+
<TextBlock Foreground="Red" Text="Sun" />
|
50
|
+
<TextBlock Text="Mon" />
|
51
|
+
<TextBlock Text="Tue" />
|
52
|
+
<TextBlock Text="Wed" />
|
53
|
+
<TextBlock Text="Thu" />
|
54
|
+
<TextBlock Text="Fri" />
|
55
|
+
<TextBlock Foreground="Blue" Text="Sat" />
|
56
|
+
</UniformGrid>
|
57
|
+
<ItemsPresenter Margin="10" />
|
58
|
+
</StackPanel>
|
59
|
+
</Border>
|
60
|
+
</ControlTemplate>
|
61
|
+
</ItemsControl.Template>
|
62
|
+
<ItemsControl.ItemsPanel>
|
63
|
+
<ItemsPanelTemplate>
|
64
|
+
<UniformGrid Columns="7" />
|
65
|
+
</ItemsPanelTemplate>
|
66
|
+
</ItemsControl.ItemsPanel>
|
67
|
+
<ItemsControl.ItemTemplate>
|
68
|
+
<DataTemplate>
|
69
|
+
<Label
|
70
|
+
HorizontalContentAlignment="Center"
|
71
|
+
VerticalContentAlignment="Center"
|
72
|
+
Content="{Binding Day}"
|
73
|
+
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" />
|
74
|
+
</DataTemplate>
|
75
|
+
</ItemsControl.ItemTemplate>
|
76
|
+
<ItemsControl.ItemContainerStyle>
|
77
|
+
<Style TargetType="ListBoxItem">
|
78
|
+
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
|
79
|
+
<Setter Property="VerticalContentAlignment" Value="Stretch" />
|
80
|
+
<Style.Triggers>
|
81
|
+
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
|
82
|
+
<Setter Property="Foreground" Value="Red" />
|
83
|
+
</Trigger>
|
84
|
+
<Trigger Property="ItemsControl.AlternationIndex" Value="6">
|
85
|
+
<Setter Property="Foreground" Value="Blue" />
|
86
|
+
</Trigger>
|
87
|
+
<DataTrigger Binding="{Binding}" Value="{x:Static sys:DateTime.MinValue}">
|
88
|
+
<Setter Property="Visibility" Value="Hidden" />
|
89
|
+
</DataTrigger>
|
90
|
+
</Style.Triggers>
|
91
|
+
</Style>
|
92
|
+
</ItemsControl.ItemContainerStyle>
|
93
|
+
</ListBox>
|
94
|
+
|
95
|
+
<!-- 表示だけの場合 -->
|
96
|
+
<ItemsControl
|
97
|
+
x:Name="calendarView"
|
98
|
+
Width="300"
|
99
|
+
AlternationCount="7">
|
100
|
+
<ItemsControl.Template>
|
101
|
+
<ControlTemplate TargetType="ItemsControl">
|
102
|
+
<ControlTemplate.Resources>
|
103
|
+
<Style TargetType="{x:Type TextBlock}">
|
104
|
+
<Setter Property="VerticalAlignment" Value="Center" />
|
105
|
+
<Setter Property="HorizontalAlignment" Value="Center" />
|
106
|
+
</Style>
|
107
|
+
</ControlTemplate.Resources>
|
108
|
+
<Border
|
109
|
+
Background="Azure"
|
110
|
+
BorderBrush="Black"
|
111
|
+
BorderThickness="1">
|
112
|
+
<StackPanel>
|
113
|
+
<UniformGrid Margin="10,10,10,0" Columns="7">
|
114
|
+
<TextBlock Foreground="Red" Text="Sun" />
|
115
|
+
<TextBlock Text="Mon" />
|
116
|
+
<TextBlock Text="Tue" />
|
117
|
+
<TextBlock Text="Wed" />
|
118
|
+
<TextBlock Text="Thu" />
|
119
|
+
<TextBlock Text="Fri" />
|
120
|
+
<TextBlock Foreground="Blue" Text="Sat" />
|
121
|
+
</UniformGrid>
|
122
|
+
<ItemsPresenter Margin="10" />
|
123
|
+
</StackPanel>
|
124
|
+
</Border>
|
125
|
+
</ControlTemplate>
|
126
|
+
</ItemsControl.Template>
|
127
|
+
<ItemsControl.ItemsPanel>
|
128
|
+
<ItemsPanelTemplate>
|
129
|
+
<UniformGrid Columns="7" />
|
130
|
+
</ItemsPanelTemplate>
|
131
|
+
</ItemsControl.ItemsPanel>
|
132
|
+
<ItemsControl.ItemTemplate>
|
133
|
+
<DataTemplate>
|
134
|
+
<Label
|
135
|
+
HorizontalContentAlignment="Center"
|
136
|
+
VerticalContentAlignment="Center"
|
137
|
+
Content="{Binding Day}"
|
138
|
+
Foreground="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContentPresenter}}" />
|
139
|
+
</DataTemplate>
|
140
|
+
</ItemsControl.ItemTemplate>
|
141
|
+
<ItemsControl.ItemContainerStyle>
|
142
|
+
<Style TargetType="ContentPresenter">
|
143
|
+
<Setter Property="Tag" Value="Black" />
|
144
|
+
<Style.Triggers>
|
145
|
+
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
|
146
|
+
<Setter Property="Tag" Value="Red" />
|
147
|
+
</Trigger>
|
148
|
+
<Trigger Property="ItemsControl.AlternationIndex" Value="6">
|
149
|
+
<Setter Property="Tag" Value="Blue" />
|
150
|
+
</Trigger>
|
151
|
+
<DataTrigger Binding="{Binding}" Value="{x:Static sys:DateTime.MinValue}">
|
152
|
+
<Setter Property="Visibility" Value="Hidden" />
|
153
|
+
</DataTrigger>
|
154
|
+
</Style.Triggers>
|
155
|
+
</Style>
|
156
|
+
</ItemsControl.ItemContainerStyle>
|
157
|
+
</ItemsControl>
|
158
|
+
|
159
|
+
<!-- カレンダーを出したいだけなら -->
|
160
|
+
<Viewbox Width="300">
|
161
|
+
<Calendar x:Name="calendar" IsHitTestVisible="False" />
|
162
|
+
</Viewbox>
|
163
|
+
</StackPanel>
|
164
|
+
|
165
|
+
<TextBlock Text="{Binding SelectedItem, ElementName=calendarSelector, StringFormat=yyyy年MM月dd日}" />
|
166
|
+
|
167
|
+
</StackPanel>
|
168
|
+
</Window>
|
169
|
+
```
|
170
|
+
|
171
|
+
```cs
|
172
|
+
using System;
|
173
|
+
using System.Linq;
|
174
|
+
using System.Windows;
|
175
|
+
using System.Windows.Controls;
|
176
|
+
|
177
|
+
namespace Questions259221
|
178
|
+
{
|
179
|
+
public partial class MainWindow : Window
|
180
|
+
{
|
181
|
+
public MainWindow()
|
182
|
+
{
|
183
|
+
InitializeComponent();
|
184
|
+
|
185
|
+
var now = DateTime.Now;
|
186
|
+
ChangeYEAR.ItemsSource = Enumerable.Range(now.Year - 10, 21);
|
187
|
+
ChangeYEAR.SelectedItem = now.Year;
|
188
|
+
|
189
|
+
ChangeMONTH.ItemsSource = Enumerable.Range(1, 12);
|
190
|
+
ChangeMONTH.SelectedItem = now.Month;
|
191
|
+
}
|
192
|
+
|
193
|
+
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
|
194
|
+
{
|
195
|
+
if(ChangeYEAR.SelectedItem is int year && ChangeMONTH.SelectedItem is int month)
|
196
|
+
{
|
197
|
+
var firstDate = new DateTime(year, month, 1);
|
198
|
+
var lastDay = firstDate.AddMonths(1).AddDays(-1).Day;
|
199
|
+
var dayOfWeek = (int)firstDate.DayOfWeek;
|
200
|
+
|
201
|
+
var spacer = Enumerable.Repeat(DateTime.MinValue, dayOfWeek);
|
202
|
+
var days = Enumerable.Range(0, lastDay).Select(x => firstDate.AddDays(x));
|
203
|
+
var items = spacer.Concat(days);
|
204
|
+
calendarSelector.ItemsSource = items;
|
205
|
+
calendarView.ItemsSource = items;
|
206
|
+
calendar.DisplayDate = firstDate;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
private void Today_Click(object sender, RoutedEventArgs e)
|
211
|
+
{
|
212
|
+
var now = DateTime.Now;
|
213
|
+
ChangeYEAR.SelectedItem = now.Year;
|
214
|
+
ChangeMONTH.SelectedItem = now.Month;
|
215
|
+
calendarSelector.SelectedItem = now.Date;
|
216
|
+
calendar.DisplayDate = now.Date;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
}
|
220
|
+
```
|
221
|
+

|
222
|
+
|
223
223
|
参考 [ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240)
|