2013-05-03 64 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