W
W
wolfak2015-05-26 14:34:10
WPF
wolfak, 2015-05-26 14:34:10

How to pass a variable to another WPF page?

Good afternoon. Ran into a new stupid question. How can you pass a variable from one XAML page to another?
Both pages were created using Visual Studio.
Page1
Page2.
You need to pass the PreID variable.
I go from the first to the second page like this:

private void pre1_Click(object sender, RoutedEventArgs e)
        {
            this.Content = new Page2();
        }

Thanks for the help. New to c#, so many stupid questions from me =)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem Voronov, 2015-05-26
@wolfak

A very bad option is through the constructor.

private void pre1_Click(object sender, RoutedEventArgs e)
        {
            this.Content = new Page2("переменная");
        }

The bad variant - through property.
private void pre1_Click(object sender, RoutedEventArgs e)
        {
            var page = new Page2();
            page.Variable = "переменная";
            this.Content = page;
        }

The human version is through a service that is pulled up using an ioc container.
private void pre1_Click(object sender, RoutedEventArgs e)
        {
            _service.Variable = "переменная";
            this.Content = new Page2();
        }

Then in Page2 like so
var service = _container.Resolve<IMyService>;
        Title = service.Variable;

Y
Yuri Devichensky, 2015-05-26
@yuvdev

Learn MVVM right away and you will be happy)

S
Sumor, 2015-05-26
@Sumor

In addition to the above, you can use the NavigationService in the application. And switch pages through it - using the Navigate method . Automatically you get the ability to store the history of transitions, taking into account all the parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question