Binding data to another window wpf

1.8k views Asked by At

I'm trying to bind something to my wpf, which consists of 2 windows. I am able to bind the data to first window, but can't really put my mind into how it should be done in the second window:

MainWindow code:

 public partial class MainWindow : Window
{
    private string repeatNumber;
    public MainWindow()
    {         
        InitializeComponent();

        string[] assignments = new string[] { "https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
        Random rnd = new Random();

        string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();


    List<Images> animals = new List<Images>();

        for (int i = 1; i < 100; i++)
        {
            if (i == 9)
            {
                repeatNumber = randomingArray[i % randomingArray.Length];
                animals.Add(new Images() { Source = repeatNumber, Number = i });
            }
            else if ((i % 9) == 0)
            {
                animals.Add(new Images() { Source = repeatNumber, Number = i });
            }
            else
            {
                animals.Add(new Images() { Source = randomingArray[i % rnd.Next(1,5)], Number = i });
            }
            ItemsControl1.ItemsSource = animals;
        }
    }


    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("test");
    }
    private void btn2_Click(object sender, RoutedEventArgs e)
    {
       Window1 window1 = new Window1();
       window1.Show();
        }
    }
}

class Images
{
    public int Number { get; set; }
    public string Source { get; set; }
}

Mainwindow xaml:

<Grid>
    <ListBox x:Name="ItemsControl1">

        <ListBox.ItemsPanel>

            <ItemsPanelTemplate>

                <UniformGrid Columns="5">
                </UniformGrid>

            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="3" Width="Auto" Height="Auto">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Number}"/>
                        <Image Source="{Binding Source}" Margin="0,0,5,0"/>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
    <Grid>
        <Button x:Name="btn1" Click="btn1_Click" Height="20" VerticalAlignment="Bottom" Margin="127,0,316,0" Content="Instruction"></Button>
        <Button x:Name="btn2" Click="btn2_Click" Height="20" VerticalAlignment="Bottom" Margin="300,0,109,0" Content="Results" Width="74"> </Button>

    </Grid>
</Grid>

Window2 code right now is just:

    public class Window2 : Window
{
  //should return the value of repeatNumber
}

xaml for windows2 right now i just:

 <Grid>


</Grid>

Im trying to get the value out of MainWindow repeatNumber. How am i able to achieve that?

1

There are 1 answers

1
BladeMaster On BEST ANSWER

You can pass it as parameter to the constructur as shown below.

public class Window2 : Window
{
  public string RepeatNumber{get;set;}

  public Window2(string repeatNumber)
  {
    RepeatNumber = repeatNumber;
  }
}

Also you can access it back in MainWindow

private void btn2_Click(object sender, RoutedEventArgs e)
{
       Window1 window1 = new Window1();
       window1.Show();
      string value =  window1.RepeatNumber; //Do what you want.
}

Best approach (my opinion) is to use MVVM Pattern for dealing with problems like yours.