2016-11-22 58 views
1

我在我的主page.xmal中有一個組合框,它從ObservableCollection填充並顯示國家名稱列表。我想要發生的是,當用戶選擇國家名稱時,我想通過一個與每個國家相關的代碼。名稱和代碼都存儲在ObservableCollection中。從一個組合框中傳遞一個值,從一個ObservableCollection填充到另一個方法UWP

我認爲我可以在組合框中設置一個屬性,它也允許我將所選項目設置爲等於國家代碼。任何幫助都會很棒。

 <StackPanel Margin="10,100,0,0" Orientation="Vertical" > 
     <ComboBox Name="countryCombo" 
      PlaceholderText="Select Country" 
      ItemsSource="{x:Bind ReadInFile.CountryCodes, Mode=OneWay}" 
      SelectedIndex="{x:Bind ReadInFile.SelectedIndex}" 
      DisplayMemberPath="Name" 
      SelectedValuePath="Code" 
      MinWidth="250" Margin="5" Opacity="0.65"> 
     </ComboBox> 
     <TextBox Name="CityTextBox" PlaceholderText="Enter City"/> 
    </StackPanel> 

private async void WeatherCityButton_Click(object sender, RoutedEventArgs e) 
    { 
     var city = CityTextBox.Text.ToString(); 
     var country = countryCombo.SelectedValuePath.ToString(); 

     RootObject myCityWeather = await WeatherFacade.GetWeatherCity(country, city); 

     WeatherResultCityText.Text = myCityWeather.current_observation.display_location.city + " _ " + myCityWeather.current_observation.temp_c.ToString() + "-" + myCityWeather.current_observation.wind_kph; 

    } 

回答

1

您已經設置了想要的方式 - SelectedValuePath完全符合您的描述。一旦設置完成,訪問ComboBox的SelectedValue屬性將提供所選項目的代碼。

你只需要改變這一行:

var country = countryCombo.SelectedValuePath.ToString(); 

要:

var country = countryCombo.SelectedValue.ToString(); 

您把它簡單地將返回「代碼」,如果我沒有記錯的話,你」的方式重新檢索您輸入的相同值。

+0

偉大的工作治療。有時很難看到樹木上的木頭。 –

+0

沒問題 - 偶爾會發生在每個人身上。請將它標記爲答案,因爲它對您有用。 :3 – Yushatak

相關問題