0

我有 2 个组合框。第一个组合框显示月份,第二个组合框显示所选月份的日期。

假设选择的月份是 Jan,选择的日期是 31。现在如果用户单击键盘向下箭头,则应该显示下一天。即月组合应该选择二月和日组合应该选择1。我怎样才能做到这一点?

当用户更改月份时,我将绑定所选月份的实际天数。我没有找到当用户尝试越过组合框限制时会触发的正确事件。即当用户在第 31 天单击向下箭头时,我如何跟踪组合框值的限制是否越过,以便我可以填充下个月的日期?

4

1 回答 1

0

一个想法是在天组合框中添加一个额外且不可见的天,并在选择它时更改所选月份。

这是您的 viewModel(您的视图的数据上下文)

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>
于 2013-11-06T10:36:09.637 回答