[英]ComboBox binding in Windows Universal App
在我的Windows通用項目中,我有ComboBoxes用於輸入時間。 我的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上設置了有效的時間,當我第一次顯示頁面時,組合框也不會像我期望的那樣自動選擇小時,分鍾和秒。 它們只是沒有顯示任何值,當我將其放下時,沒有選擇任何項目。 我嘗試改為使ObservableCollections int為基礎,但結果相同。 我在這里想念什么?
這里的問題是在設置ItemsSource
之前先設置SelectedItem
。 您可能會認為這並不重要,但不幸的是,它確實如此。 在此說明原因。
將您的代碼更改為:
<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>
初始化ObservableCollections
后,嘗試設置Time
。 我認為這是因為設置Time
ComboBox中沒有任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.