2014-12-04 141 views
0

我正在C#wpf上工作,并试图在其上创建'最喜欢的'功能。 有一个窗口叫'最爱',它是用户可以保存他的数据的地方。 另一个窗口是'MainWindow',我想在这里加载数据。如何将数据从一个窗口传递到另一个窗口?

'收藏'中有两个按钮和两个文本框。 如果我在每个框中键入单词,我希望他们可以保存,如果我点击star1button。 如果我在每个框中键入另一个单词,我希望他们将被保存,如果我点击star2button。 所以我希望每个数据都会分开存储,不会重叠。

然后,如果我在'mainwindow'上按下按钮1,我希望star1button的这些单词会显示在mainwindow的文本框中。 如果我按mainwindow上的button2,我希望star2button的文字会出现在主窗口的文本框中。

在此先感谢!

+0

发布您的代码! – Mangesh 2014-12-04 10:43:41

+0

这是一个广泛的话题来讨论如果你想正确地做。我建议阅读关于MVVM模式(http://msdn.microsoft.com/en-us/magazine/dd419663.aspx) – Philippe 2014-12-04 10:51:28

回答

0

您可以将您的App.xaml.cs的属性,它应该是从两个

namespace MyApp 
{ 
    sealed partial class App : Application 
    { 
     public string myValue; 

     // the rest of your App.xaml.cs code 
    } 
} 

然后在你的主窗口和其他窗口把这个代码

public string MyValue 
    { 
     get 
     { 
      return (Application.Current as MyApp.App).myValue; 
     } 
     set 
     { 
      (Application.Current as MyApp.App).myValue= value; 
     } 
    } 
0

您可以使用访问静态类在窗口之间传递值。

public static class CurrentParameters 
{ 
    public static string mySharedValue { get; set; } 
} 
相关问题