从另一个页面
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“。
问题解决了:
当导航到其他页面我传递的值:
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;
}
Combobox
有SelectedIndex
财产。通过此属性,您可以在另一个页面中获取选定的值。我在我的MVVM中使用它。只需将此属性绑定到我的ViewModel
中的某个公共属性,然后我就可以在其他地方使用它。而且这种绑定可能是两种方式。
ComboBox cboTeam1位于第一页,TextBlock Team1位于第二页。 Team1需要获取cboTeam1中所选项目的值。 试过这个:
您需要为这两个页面具有相同的'DataContext'。我使用第三课来存储这些属性。这第三个类实现了'INotifyPropertyChanged'接口。现在你可以在这个第三类的'int'属性绑定'ComboBox'和'TextBlock'。 – Artholl 2013-05-01 11:41:16
你需要更具体一点?这些包含什么?他们来自哪里? (I.eSQL数据库,数据表,用户输入)等,但你也需要引用这些,以便你可以打电话给他们。我的建议是研究这更多 – 2013-05-01 08:08:13
我编辑了我的文章。 – user1951083 2013-05-01 08:15:06