Windows Universal App中的ComboBox绑定

时间:2016-05-20 13:07:29

标签: c# mvvm combobox win-universal-app

在我的Windows Universal Project中,我有ComboBoxes用于时间输入。三个ComboBox绑定到我的ViewModel中的Hour,Minute和Second属性...

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <ComboBox SelectedItem="{Binding Hour, Mode=TwoWay}" ItemsSource="{Binding HourList}"></ComboBox>
    <ComboBox SelectedItem="{Binding Minute, Mode=TwoWay}" ItemsSource="{Binding MinuteList}"></ComboBox>
    <ComboBox SelectedItem="{Binding Second, Mode=TwoWay}" ItemsSource="{Binding SecondList}"></ComboBox>
</StackPanel>

这是我的ViewModel ...(注意我最终希望我的表单能够设置和显示“Time”属性)....

public class TimeEntryViewModel : INotifyPropertyChanged
{
    public bool Accept { get; set; }

    public TimeEntryViewModel(DateTime time)
    {
        Time = time;
        HourList = new ObservableCollection<string> { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };
        MinuteList = new ObservableCollection<string> { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" };
        SecondList = new ObservableCollection<string> { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" };
    }

    public ObservableCollection<string> HourList { get; set; }
    public ObservableCollection<string> MinuteList { get; set; }
    public ObservableCollection<string> SecondList { get; set; }

    private string _hour;
    public string Hour {
        get { return _hour; }
        set
        {
            _hour = value;
            OnPropertyChanged("Hour");
        }
    }

    private string _minute;
    public string Minute
    {
        get { return _minute; }
        set
        {
            _minute = value;
            OnPropertyChanged("Minute");
        }
    }
    private string _second;
    public string Second
    {
        get { return _second; }
        set
        {
            _second = value;
            OnPropertyChanged("Second");
        }
    }

    public DateTime Time
    {
        get
        {
            return new DateTime(DateTime.Now.Year, 
                DateTime.Now.Month, 
                DateTime.Now.Day, 
                int.Parse(_hour), int.Parse(_minute), int.Parse(_second));
        }
        set
        {
            Hour = value.Hour.ToString("00");
            Minute = value.Minute.ToString("00");
            Second = value.Second.ToString("00");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

问题在于,当我第一次显示页面时,即使我在ViewModel上设置了有效时间,Combos也不会像我期望的那样自动选择小时,分钟和秒。它们只显示没有值,当我放下它们时,没有选择任何项目。我试过改为使用ObservableCollections int,但结果相同。我在这里缺少什么?

2 个答案:

答案 0 :(得分:2)

问题是,您在设置SelectedItem之前首先设置ItemsSource。你可能认为它并不重要,但不幸的是,确实如此。 Here解释了原因。

将您的代码更改为:

<StackPanel Orientation="Horizontal"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
    <ComboBox ItemsSource="{Binding HourList}"
              SelectedItem="{Binding Hour, Mode=TwoWay}"></ComboBox>
    <ComboBox ItemsSource="{Binding MinuteList}"
              SelectedItem="{Binding Minute, Mode=TwoWay}"></ComboBox>
    <ComboBox ItemsSource="{Binding SecondList}"
              SelectedItem="{Binding Second, Mode=TwoWay}"></ComboBox>
</StackPanel>

答案 1 :(得分:0)

初始化Time后尝试设置ObservableCollections。我认为这是因为设置Time时组合框中没有任何内容。