2013-05-01 81 views
0

Windows 8的應用程序C#從另一個頁面

我有一個頁面上的文本框數和ComboBox控件獲取控件的值。在另一頁上,我希望獲得這些控件的值以再次使用它們(例如cboTeam.SelectedValue)。

有沒有一種正確的方法來獲取這些值在其他頁面?

編輯:

XAML代碼:

<ComboBox ItemTemplate="{StaticResource Test}" 
      ItemsSource="{Binding test}" 
      x:Name="cboTeam1" 
      Grid.Column="2" Grid.Row="4" 
      Margin="0,13,0,12"/> 

獲取的ItemsSource:

cboTeam1.ItemsSource = ItemRepository.getJPLItems(); 

在我的倉庫:

public static List<TeamItem> getJPLItems() 
{ 
    if (JPLItems.Count == 0) 
    { 
     JPLItems.Add(new TeamItem() { Id = 1, Description = "Anderlecht", Image = "Jpl/Anderlecht.png", ItemType = ItemType.JupilerProLeague }); 
     JPLItems.Add(new TeamItem() { Id = 1, Description = "Beerschot", Image = "Jpl/Beerschot.png", ItemType = ItemType.JupilerProLeague }); 
     JPLItems.Add(new TeamItem() { Id = 1, Description = "Cercle Brugge", Image = "Jpl/Cercle Brugge.png", ItemType = ItemType.JupilerProLeague }); 
    } 
} 

所以我cboTeam1充滿了「 Anderl echt「,」Beerschot「,」Cercle Brugge「。

+0

你需要更具體一點?這些包含什麼?他們來自哪裏? (I.eSQL數據庫,數據表,用戶輸入)等,但你也需要引用這些,以便你可以打電話給他們。我的建議是研究這更多 – 2013-05-01 08:08:13

+0

我編輯了我的文章。 – user1951083 2013-05-01 08:15:06

回答

0

問題解決了:

當導航到其他頁面我傳遞的值:

var selected = cboTeam1.SelectedValue as TeamItem; 
    getInfo gI = new SportsBetting.Scoreandinvite.getInfo() { Team1 = selected.Description}; 

    Frame.Navigate(typeof(Scoreandinvite), gI); 

在我創建了一個類的其他頁面:

public class getInfo 
    { 
     public string Team1 
     { 
      get; 
      set; 
     } 
    } 

在OnNavigatedTo方法:

protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     getInfo gI = (getInfo)e.Parameter; 
     Team1.Text = gI.Team1; 
    } 
1

ComboboxSelectedIndex財產。通過此屬性,您可以在另一個頁面中獲取選定的值。我在我的MVVM中使用它。只需將此屬性綁定到我的ViewModel中的某個公共屬性,然後我就可以在其他地方使用它。而且這種綁定可能是兩種方式。

+0

ComboBox cboTeam1位於第一頁,TextBlock Team1位於第二頁。 Team1需要獲取cboTeam1中所選項目的值。 試過這個:但沒有結果。 – user1951083 2013-05-01 09:53:28

+0

您需要爲這兩個頁面具有相同的'DataContext'。我使用第三課來存儲這些屬性。這第三個類實現了'INotifyPropertyChanged'接口。現在你可以在這個第三類的'int'屬性綁定'ComboBox'和'TextBlock'。 – Artholl 2013-05-01 11:41:16

相關問題