2013-06-27 84 views
-1

我使用「的Visual Studio Express Windows版8」,用組合框控件作爲Windows 8的組合框PlaceholderText

<ComboBox Name="Categories" > 
       <x:String>apple</x:String> 
       <x:String>ball</x:String> 
       <x:String>cat</x:String> 
       <x:String>dog</x:String>      
      </ComboBox> 

我想表明它佔位符文本顯示一些文字,直到用戶還沒有選擇任何從它的項目。但是當我使用屬性PlaceholderText在microsoft reference描述顯示文本但是當我使用它的SDK顯示了這個錯誤

成員「PlaceholderText」無法識別或不能訪問。

或有任何其他方法,以便我可以在Combobox中顯示一些默認文本。 謝謝。

回答

0

這是用於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) 
    { 
    } 
} 
+0

是的,我知道,但請告訴我如何在我的Windows 8應用程序中使用佔位符屬性。 – Jatin

+0

@Ricky看例子 – OmegaMan