2013-05-03 53 views
0

選擇組合框的項目我有一個組合框負荷漫遊設置

<ComboBox x:Name="cityPicker"> 
    <ComboBoxItem IsSelected="True"> 
     <x:String> 
      city1 
     </x:String> 
    </ComboBoxItem> 
    <ComboBoxItem> 
     <x:String> 
      city2 
     </x:String> 
    </ComboBoxItem> 

當用戶選擇「城2」,那麼我將它保存到漫遊設置在selectedCity關鍵。

我需要從漫遊設置中加載這個值,當用戶在退出後啓動應用程序時以及從另一個啓動應用程序返回到此頁面後。

將此代碼值保存到RoamingSetting,並且當我在更改城市後啓動應用程序時,roamingsettings具有它的價值。但Combobox不檢索它。組合框選定的項目保持空白。

如何以編程方式更改組合框中的選定項目?

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) 
{ 
    var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; 
    if (roamingSettings.Values.ContainsKey("selectedCity")) 
    { 
     cityPicker.SelectedValue = roamingSettings.Values["selectedCity"].ToString(); 
    } 
} 

public StartPage() 
     { 
      InitializeComponent(); 

      cityPicker.SelectionChanged += cityPicker_SelectionChanged; 
     } 

     void cityPicker_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      var roamingSettings = 
       Windows.Storage.ApplicationData.Current.RoamingSettings; 
      var cityPick = cityPicker.SelectedItem as ComboBoxItem; 
      if (cityPick != null) roamingSettings.Values["selectedCity"] = cityPick.Content; 
     } 

我只能通過改變SelectedIndex來做到這一點。但這不是我想要的。

+0

哪裏是保存數據的代碼? – 2013-05-03 18:13:46

+0

我更新問題。在調試模式下值保存成功,roamingSettings.Values [「selectedCity」]包含所需的值。 – TheX 2013-05-03 18:41:54

+0

那麼,有什麼問題?你的文章沒有特別好的解釋。 – 2013-05-03 19:52:25

回答

1

所以,我想我知道這裏發生了什麼。您已將ComboBox填入ComboBoxItems,並稍後嘗試將其SelectedValue設置爲string。當你這樣做時,ComboBox檢查它是否包含新值。它使用ComboBoxItem.Equals()執行此檢查,檢查引用相等。顯然,由於被比較的兩個對象的類型不同,因此該檢查將始終返回false,這就是爲什麼ComboBox無法找到並顯示它。這裏要做的正確的事情是將您的ComboBoxItemsSource設置爲強類型的字符串集合。如果這樣做,ComboBox將使用String.Equals()進行相等性檢查,該檢查執行使用值類型相等語義的相等性。

假設您的ComboBoxname設置爲「comboBox」。在您的代碼隱藏事件處理程序處理程序:

IEnumerable<string> foo = new[] { "A", "B", "C" }; 
comboBox.ItemsSource = foo; 
comboBox.SelectedValue = "B"; // This should work 

這裏是它直接關係到你的代碼的例子

XAML:

<ComboBox x:Name="cityPicker" /> 

C#:

public StartPage() 
{ 
    InitializeComponent(); 
    cityPicker.ItemsSource = new[] { "city1", "city2" }; 
    cityPicker.SelectionChanged += OnCityPickerSelectionChanged; 
} 

void OnCityPickerSelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    // get your roaming settings 
    var selectedItem = cityPicker.SelectedItem; 
    roamingSettings.Values["SelectedCity"] = (string) selectedItem; 
} 

protected override void LoadState(...) 
{ 
    // get roaming settings, perform key check 
    cityPicker.SelectedValue = (string) (roamingSettings.Values["SelectedCity"]); 
} 

顯然,「正確「的方式是做一個視圖模型來暴露ObservableCollection<string>。您可以將此視圖模型設置爲數據上下文,然後在您的ItemsSource與視圖模型ObservableCollection<string>之間設置綁定。儘管這可能是一個不同的日子!

+0

感謝您的幫助。如果我把它放在InitializeComponent()之後,你的代碼工作得很好。但它不起作用,如果我在LoadState方法中將comboBox.SelectedValue =「B」。所以...我很困惑。 – TheX 2013-05-03 21:11:23

+0

確保你沒有將它存儲爲'ComboBoxItem',而是作爲一個字符串存儲。請記住,您希望在這裏使用值類型相等語義!確保這行'var cityPick = cityPicker.SelectedItem作爲ComboBoxItem;'是'var cityPick = cityPicker.SelectedItem作爲字符串;' – 2013-05-03 21:13:48

+0

是的,我敢肯定...我嘗試了combobox.SelectedIndex及其在LoadState方法中的良好工作。我不明白什麼是錯的。 – TheX 2013-05-03 21:15:24