JakobMiller
JakobMiller

Reputation: 353

Send data between WPF Windows

So I have been looking around and haven't found a solution for this, using WPF Windows.

So what I want to do is to send values from two sliders from one WPF Window to another. How should this be done using two WPF Windows inside the same project? And is it possible to do it from another project inside the same solution using references?

I have been working with Windows forms and can get it to work easily there, but it just won't work with WPF Windows.

[Edited] What needs to be done is very basic really. We have a menu Project called Startup with a MainWindow.xaml that does not yet contain any code that needs to be mentioned. Although, we want from the menu, to be able to use 2 sliders, 1 to set the amount of players that are going to participate and another to set the amount of hours that the game should last. We have fixed this problem already from the game itself by setting the amount of players like this:

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)  // Change move-army-strenght
{
    movearmy = Convert.ToInt32(slider1.Value);
    lblresult3.Content = "" + movearmy;
    if (menuphase == 1) { players = Convert.ToInt32(slider1.Value); }
}

This will check if it's menuphase one and then get the amount of players from that. Although, we want to send this value from the Menu, not while inside the game!

Upvotes: 3

Views: 2777

Answers (2)

The OP wrote in an edit:

[SOLUTION]

public partial class SelectionWindow : Window
{
    WpfApplication1.GameWindow newForm2;

    public SelectionWindow()
    { InitializeComponent(); }

    private void continue_Click(object sender, RoutedEventArgs e)
    {
        if (Convert.ToInt32(slider1.Value) > 1)
        {
            Worldmap.Properties.Settings.Default.value1 = Convert.ToInt32(slider1.Value);
            Worldmap.Properties.Settings.Default.value2 = Convert.ToInt32(slider2.Value);
            Worldmap.Properties.Settings.Default.Save();
            newForm2 = new WpfApplication1.GameWindow();
            newForm2.Show();
            Close();
        }
    }

    private void slider1_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e)
    { playerresult.Content = Convert.ToInt32(slider1.Value); }

    private void slider2_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e)
    { timeresult.Content = Convert.ToInt32(slider2.Value); }
}

Upvotes: -1

Dave Clemmer
Dave Clemmer

Reputation: 3781

It may well be overkill for your requirements, but an extensible way to send messages with data between forms, windows, etc. is to use the Mediator Design Pattern. There is an excellent article and sample project by Marlon Grech for a WPF Mediator.

This pattern works well for sending a variety of messages, so long as there aren't too many subscribers looking for the messages.

Upvotes: 2

Related Questions