简体   繁体   中英

WPF .how can i access to fields in other Forms without get new object

I have a MainWindow and a DetailedBookView . i want use Method and field from DetailedBookView in MainWindow class whit out using new object... MainWindows isn't parent of DetailedBook ... please help me to write code in MainWindow to use DataContaxt in MainWindow.

public partial class MainWindow : Window
{
    //use DataContaxt method here without using new object
    enter code here

}


public partial class DetailedBookView : UserControl
{
    int DataContaxt = 10;
}

也许只是将DataContext字段设置为静态

Make Method and fields which you want to use in another class of DetailedBookView static . Doing so, you can use these methods and fields with the class name directly.

public partial class DetailedBookView : UserControl
{
    static int DataContaxt = 10;
}

Now you can use DataContaxt directly as:

public partial class MainWindow : Window
{    
    int a = DetailedBookView.DataContaxt;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM