这适用于 Windows 8.1 预览版,而不是 Windows 8 开发。此时您需要安装预览版,然后才能使用此组合框进行开发。查看占位符的文档,它指出:
Minimum supported client Windows 8.1 Preview
编辑
要手动执行此操作,只需手动预加载组合框。这是一个示例,让我们从 ViewModel 开始,其中构造函数将初始值加载到名为“Loading”的组合框中
public class MainVM : INotifyPropertyChanged
{
private List<string> _dataList;
public List<string> ComboData
{
get { return _dataList; }
set
{
if (_dataList != value)
{
_dataList = value;
OnPropertyChanged();
}
}
}
public MainVM()
{
ComboData = new List<string> {"Loading..."};
}
#region INotify Property Changed Implementation
/// <summary>
/// Event raised when a property changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="propertyName">The name of the property that has changed.</param>
public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
现在在主页面上,xaml 绑定到 ComboData,但我们需要警惕第一种情况,即加载一个项目的列表,并且我们希望将其设为选定项目。
<ComboBox ItemsSource="{Binding ComboData}" Height="30" Width="300" Loaded="OnLoaded" />
好的,在页面后面的代码中,我们将 datacontext 设置为我们之前设置的 ViewModel,但还有一个 OnLoaded 方法来检查 1 项加载情况。在下面的示例中,我们模拟了加载其余数据的 3 秒延迟。
public sealed partial class MainPage : Page
{
public MainVM ViewModel { get; set; }
public MainPage()
{
this.InitializeComponent();
DataContext = ViewModel = new MainVM();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
var cBox = sender as ComboBox;
if (cBox != null)
{
if ((cBox.Items != null) && (cBox.Items.Count == 1))
{
cBox.SelectedIndex = 0;
// Debug code to simulate a change
Task.Run(() =>
{
// Sleep 3 seconds
new System.Threading.ManualResetEvent(false).WaitOne(3000);
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{ ViewModel.ComboData = new List<string> {"Alpha", "Gamma", "Omega"}; });
});
}
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}