1 つのアイデアは、余分な非表示の日から日へのコンボボックスを追加し、それが選択されたときに選択された月を変更することです。
これはあなたのviewModel(あなたのビューのdataContext)です
public class ViewModel : DependencyObject
{
public ViewModel()
{
for (int i = 0; i < 12; i++)
{
Months.Add(new Month(i+1, 31, this));//this is simplified
}
SelectedMonth = Months.First();
}
//Months Observable Collection
private ObservableCollection<Month> _months = new ObservableCollection<Month>();
public ObservableCollection<Month> Months { get { return _months; } }
//SelectedMonth Dependency Property
public Month SelectedMonth
{
get { return (Month)GetValue(SelectedMonthProperty); }
set { SetValue(SelectedMonthProperty, value); }
}
public static readonly DependencyProperty SelectedMonthProperty =
DependencyProperty.Register("SelectedMonth", typeof(Month), typeof(ViewModel), new UIPropertyMetadata(null));
}
これはあなたの月のクラスです
public class Month : DependencyObject
{
public Month(int number, int count, ViewModel parent)
{
_parent = parent;
Number = number;
Days.Add(new Day { IsLastDayOfMonth = false, IsFirstDayOfMonth = true });
for (int i = 0; i < count; i++)
{
Days.Add(new Day {
Number = i + 1,
IsLastDayOfMonth = false,
IsFirstDayOfMonth = false });
}
Days.Add(new Day { IsLastDayOfMonth = true, IsFirstDayOfMonth = false });
SelectedDay = Days[1];//first is empty so by default day1 is selected
}
ViewModel _parent;
//Number Dependency Property
public int Number
{
get { return (int)GetValue(NumberProperty); }
set { SetValue(NumberProperty, value); }
}
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register("Number", typeof(int), typeof(Month), new UIPropertyMetadata(1));
//Days Observable Collection
private ObservableCollection<Day> _days = new ObservableCollection<Day>();
public ObservableCollection<Day> Days { get { return _days; } }
//SelectedDay Dependency Property
public Day SelectedDay
{
get { return (Day)GetValue(SelectedDayProperty); }
set { SetValue(SelectedDayProperty, value); }
}
public static readonly DependencyProperty SelectedDayProperty =
DependencyProperty.Register("SelectedDay", typeof(Day), typeof(Month),
new UIPropertyMetadata(null, (d, e) =>
{
var viewModel = ((Month)d)._parent;
if (((Day)e.NewValue).IsLastDayOfMonth)
{
var selMonth = viewModel.Months.FirstOrDefault(x => x.Number == ((Month)d).Number + 1);
if (selMonth == null || selMonth.Number == 1) return;
viewModel.SelectedMonth = selMonth;
viewModel.SelectedMonth.SelectedDay =
viewModel.SelectedMonth.Days[1];//first real day of month
}
else if (((Day)e.NewValue).IsFirstDayOfMonth)
{
var selMonth = viewModel.Months.FirstOrDefault(x => x.Number == ((Month)d).Number - 1);
if (selMonth == null) return;
viewModel.SelectedMonth = selMonth;
viewModel.SelectedMonth.SelectedDay =
viewModel.SelectedMonth.Days[
viewModel.SelectedMonth.Days.Count - 2];//last real day of month
}
}));
}
これはあなたのデイクラスです
//Number Dependency Property
public int Number
{
get { return (int)GetValue(NumberProperty); }
set { SetValue(NumberProperty, value); }
}
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register("Number", typeof(int), typeof(Day), new UIPropertyMetadata(1));
//IsLastDayOfMonth Dependency Property
public bool IsLastDayOfMonth
{
get { return (bool)GetValue(IsLastDayOfMonthProperty); }
set { SetValue(IsLastDayOfMonthProperty, value); }
}
public static readonly DependencyProperty IsLastDayOfMonthProperty =
DependencyProperty.Register("IsLastDayOfMonth", typeof(bool), typeof(Day), new UIPropertyMetadata(false));
public bool IsFirstDayOfMonth
{
get { return (bool)GetValue(IsFirstDayOfMonthProperty); }
set { SetValue(IsFirstDayOfMonthProperty, value); }
}
public static readonly DependencyProperty IsFirstDayOfMonthProperty =
DependencyProperty.Register("IsFirstDayOfMonth", typeof(bool), typeof(Day), new UIPropertyMetadata(false));
そして、これはあなたのxamlです:
<ComboBox ItemsSource="{Binding SelectedMonth.Days}"
SelectedItem="{Binding SelectedMonth.SelectedDay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Number}" x:Name="textblock"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsLastDayOfMonth}" Value="True">
<Setter TargetName="textblock" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>