将数据绑定到另一个窗口wpf

时间:2017-11-17 13:35:48

标签: c# wpf xaml

我正在尝试将某些东西绑定到我的wpf,它由2个窗口组成。我能够将数据绑定到第一个窗口,但是无法真正理解如何在第二个窗口中完成它:

MainWindow代码:

 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代码只是:

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

对于Windows2的xaml我现在只是:

 <Grid>


</Grid>

我试图从MainWindow repeatNumber中获取值。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以将其作为参数传递给构造函数,如下所示。

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

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

您也可以在MainWindow中访问它

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

最佳方法(我的观点)是使用MVVM模式来处理像你这样的问题。