문제

I'm using c# and WPF web application and I want to build a simple form which contains two text boxes for user input (name and phone number) and a "send" button. when the user clicks on the send button it will redirect to an other page and will display (with textblock) the entered values.

I tried to read about data binding, but still didn't success to make it work.
1. How do I save the entered values into a variable ?
2. How do I call these variables from the second page and display the saved text ?

Hope for help, thanks!

xaml code of the form:

<TextBlock Height="20" Width="120" Margin="36,43,144,237">enter details:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,69,224,211">Name:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,103,224,177">Phone:</TextBlock>
<!--textboxes-->
<TextBox Height="20" Width="95" Margin="100,69,104,211" Name="getName" Background="Gray"/>
<TextBox Height="20" Width="95" Margin="100,103,105,177" Name="getPhoneNumber" Background="Gray"/>
<!--          -->
<Button Height="20" Width="50" Margin="218,103,32,177" Name="sendButton" Click="sendButton_Click">send</Button>

what should be the code behind ? i simply want to display the entered values on an other page with textblock. this is the function i use to redirect to the "result page":

public void sendButton_Click(object sender, RoutedEventArgs e)
{
    Submit();
}

void Submit()
{
    Page2 resultpage = new Page2();
    NavigationService.Navigate(resultpage);
}

Edit:
Ok I succeed to make it work thanks to Mike's answer.

Thanks Mike, for other users this how the code looks like now:
form page xaml code:

<TextBlock Height="20" Width="120" Margin="36,43,144,237">enter details:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,69,224,211">Name:</TextBlock>
<TextBlock Height="20" Width="40" Margin="36,103,224,177">Phone:</TextBlock>
<!--textboxes-->
<TextBox Height="20" Width="95" Margin="100,69,104,211" Name="getName" Background="Gray"/>
<TextBox Height="20" Width="95" Margin="100,103,105,177" Name="getPhoneNumber" Background="Gray"/>
<!--          -->
<Button Height="20" Width="50" Margin="218,103,32,177" Name="sendButton" Click="sendButton_Click">send</Button>

form page c# behind code:

    public partial class Page1 : Page
{
    public Page1()
    {
        InitializeComponent();
    }

    public void sendButton_Click(object sender, RoutedEventArgs e)
    {
        Submit();
    }

    void Submit()
    {
        Page2 resultpage = new Page2(getName.Text, getPhoneNumber.Text);
        NavigationService.Navigate(resultpage);
    }
}

result page xaml code:

    <Grid>
    <TextBlock x:Name="showName" Height="50" Width="100" Margin="65,125,73,125" Text="{Binding ElementName=showName, Path=Text}" />
</Grid>

result page c# code behind:

        public Page2(string name, string phoneNumber)
    {
        InitializeComponent();
        showName.Text = name;
    }

Thanks again Mike :)

도움이 되었습니까?

해결책

Ok here is what I would do. You can create arguments in the constructor of your Page2:

public Page2(string name, string phoneNumber)
{
    //login to handle name and phone number

}

On your first page you can just pass name and phone number using the Name property in the xaml.

void Submit()
{
    Page2 resultpage = new Page2(getName.Text, getPhoneNumber.Text);
    NavigationService.Navigate(resultpage);
}

다른 팁

<Page x:Class="Login.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  d:DesignHeight="253" d:DesignWidth="276"
Title="Page1" Background="Blue">
  <Grid>
    <TextBlock Text="Prince jain" HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" Width="100" Margin="48,0,0,0" FontSize="40"></TextBlock>
    <TextBlock Height="50" FontSize="18" HorizontalAlignment="Left" Margin="6,141,0,0" Name="txtnavigatevalue" Text="Test Navigation" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="134,144,0,0" Name="txtnavigation" VerticalAlignment="Top" Width="130" />
    <Button Content="Check" Height="23" HorizontalAlignment="Left" Margin="134,187,0,0" Name="buttoncheck" VerticalAlignment="Top" Width="75" Click="buttoncheck_Click" />
  </Grid>
</Page>

<Page x:Class="Login.Page2"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-      compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page2" Background="BlueViolet">

  <Grid>
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="46,94,0,0"                         Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="198" />
  </Grid>
</Page>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Login
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void buttoncheck_Click(object sender, RoutedEventArgs e)
        {

            Page2 p2 = new Page2();
            p2.textBlock1.Text = txtnavigation.Text;
            //txtnavigatevalue.Text = "";
            NavigationService.Navigate(p2);
        }
    }
}
private void buttoncheck_Click(object sender, RoutedEventArgs e)
{
    Page2 p2 = new Page2();
    p2.textBlock1.Text = txtnavigation.Text;
    //txtnavigatevalue.Text = "";
    NavigationService.Navigate(p2);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top