单击按钮时,导航到带有组合框项目的新页面

时间:2018-08-17 11:03:46

标签: c# uwp

我希望用户在组合框中选择一个项目,当他们按下按钮时,它们将被重定向到特定页面。我该如何实现?

<pre><Grid>
        <ComboBox x:Name="Campus_Selector" HorizontalAlignment="Center" Margin="0,414,0,0" VerticalAlignment="Top" PlaceholderText="Select a Campus " Width="233">
            <ComboBoxItem Content="Auckland Park" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100" />
            <ComboBoxItem Content="Bloemfontein" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Boksburg" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Cape Town" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Durban" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Nelspruit" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Polokwane" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Port Elizabeth" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Potchefstroom" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Pretoria" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Roodepoort" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Sandton" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Stellenbosch" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
            <ComboBoxItem Content="Vereeniging" HorizontalAlignment="Center" Height="100" Margin="0,0,0,0" VerticalAlignment="Center" Width="100"/>
        </ComboBox>
        <Button x:Name="Navtocapus" Content="Navigate to selected Campus" HorizontalAlignment="Left" Margin="100,504,0,0" VerticalAlignment="Top" Click="Navtocapus_Click"/>

    </Grid>

我尝试使用if语句,并且尝试了所有的Campus_Selector.Selected(statements)

2 个答案:

答案 0 :(得分:1)

  

点击按钮后,只需检查组合框选定属性并进行相应导航。

private void Navtocapus_Click(object sender, args)
{
    if(Campus_Selector.SelectedIndex != -1)//just to make sure an item is selected
    {
        string item = (Campus_Selector.SelectedItem as ComboboxItem).Content as string;
        //use whatever your frame is to navigate to your desired page using value of item.
        frame.Navigate(typeof(YourPageClass));
    }
}

答案 1 :(得分:0)

转到您的Button_Click事件,从Combobox.SelectedIndex创建一个SelectCase, 并处理每种情况:

Select case Combobox.SelectedIndex;
case 1;
Frame.Navigate(SomePage);
case 2;
Frame.Navigate(SomeotherPage);
...