在WPF C#中将对象信息发送到另一个窗口

时间:2015-12-13 22:38:20

标签: c# wpf window

c是一个具有某些属性的对象,name是其中之一。我正在创建一个包含所选对象名称的文本块。我试图通过发送所有对象的信息来跳转到新页面;我希望能够获得对象的信息" c"在另一个窗口。

public MainWindow()
    {      
        InitializeComponent();
    }
private void b1_Click(object sender, RoutedEventArgs e)
    {
TextBlock t1 = new TextBlock();
        t1.Text = c.getName();
        s1.Children.Add(t1);
Run run3 = new Run(c.getName());

        Hyperlink hyperlink = new Hyperlink(run3)
        {
            NavigateUri = new Uri("http://stackoverflow.com")
        };
        hyperlink.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(Hyperlink_RequestNavigate); //to be implemented

        t1.Inlines.Clear();
        t1.Inlines.Add(hyperlink);


    }

    private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Window1 win1 = new Window1();
        win1.Show();
        this.Hide();
    }

}

 public Window1()
    {
        InitializeComponent();

        //i want to obtain the object c here
        Console.write( c.getName() );
        Console.write( c.getAddress() );
        Console.write( c.getPrice() );
                  etc....
    }

1 个答案:

答案 0 :(得分:1)

Window1添加一个构造函数,该构造函数引用Object'c'

public Window1(ObjectC c)
{
    InitializeComponent();

    //i want to obtain the object c here
    Console.write( c.getName() );
    Console.write( c.getAddress() );
    Console.write( c.getPrice() );
              etc....
}

然后更新:

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
    ObjectC c = new ObjectC();
    Window1 win1 = new Window1(c);
    win1.Show();
    this.Hide();
}