這是用於Windows 8.1預覽而不是Windows 8開發。您需要在此時安裝預覽,然後才能使用此組合框進行開發。縱觀文檔的佔位符,它指出:
Minimum supported client Windows 8.1 Preview
編輯
手動操作也很簡便預裝手工組合框。下面是一個例子,讓我們先從視圖模型在構造函數將加載一個初始值到名爲在主頁XAML綁定到ComboData「加載」
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
}
現在組合框,但我們需要警惕的第一種情況,其中一個項目的列表將被加載,並且我們想要做出所選擇的項目。
<ComboBox ItemsSource="{Binding ComboData}" Height="30" Width="300" Loaded="OnLoaded" />
好,在頁面的後臺代碼,我們將設置我們的datacontext之前要在視圖模型,我們的設置,但也有其檢查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)
{
}
}
是的,我知道,但請告訴我如何在我的Windows 8應用程序中使用佔位符屬性。 – Jatin
@Ricky看例子 – OmegaMan