I have a text box in the MainWindow. I was wondering how I can access it from another window. I tried declaring an instance of the MainWindow but it doesn't work. Say I want to set the text in the MainWindow to something, in SecondWindow. How can I do that? Thanks!
Expose a public function on MainWindow that will allow you to set some property on it. Then when creating the new window pass an instance of MainWindow to its constructor. Now from within this new window call the public function you have exposed.
Related
I have a wpf application which will work minimized. The application will show a second window when system wakes up from sleep. In the second window there is a combobox and a button. when i click on the button it should set the value of a variable in the mainwindow with the value of combobox. But the problem is the variable in mainwindow is not accessible in second window. How to do this?? I searched a lot in net. But unable to find a working solution. Any suggestions??
You could use Event / delegate approach to get it:
A code snippet to sum-up:
In your First window when creating the second one do
Window1 win = new Window1();
win.GetEvent += win_GetEvent;
win.ShowDialog();
And in the second window you have:
public object ValueToGet;
public delegate object GetValueDelegate(object _value);
public event GetValueDelegate GetEvent;
public Window1()
{
InitializeComponent();
GetEvent.Invoke(ValueToGet);
}
Well I don't know which your specific requirment but just to intreduce the approach
I am creating a UserControl, I want that when I click in a button from that Control a property (attriibute) modifies from my MainWindow. The UserControl is created from a separate project and built as a .dll.
I had tried the following:
Window l = Window.GetWindow(this);
The problem is that because my window is not being referenced I have no way to access it (the properties I had created) and I dont know how to do it. If I try to write "MainWindow" it says that it couldn't be found.
You can get window using Application.Current.MainWindow. It will return window object so make sure you typecast it to actual instance of your window.
Assuming actual instance is MainWindow, it can be accessed like this:
MainWindow window = (MainWindow)Application.Current.MainWindow;
You have a number of ways of accessing a reference to the main Window in WPF. There is the way that #Rohit Vats showed you:
MainWindow window = (MainWindow)Application.Current.MainWindow;
However, as you have noticed, this does not always work. Sometimes it can be fixed simply by setting the property to the MainWindow instance:
public MainWindow()
{
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Application.Current.MainWindow = this;
}
You should now be able to access the MainWindow from this property. However, if that still doesn't work for some reason, then you can also try the Application.Windows property:
foreach (MainWindow window in Application.Windows.OfType<MainWindow>())
{
// Do something with window here
}
I have two windows ,say window1 and window2(this one should be a pop up) on WPF.
What I want to do is, when a button in popup window(window2) is clicked,
I want to run method in window1.
I can achieve this by passing window1 to windows2, but I think it's not an memory-efficient way.
I have red article about routedCommand, but it's hard to understand.
I'm working on c# and any help is appreciated
Thank you
Quite often I have a static property for Current on my MainViewModel (or which ever ViewModel), and I set that property in the constructor for the ViewModel. Then from anywhere else in the application, I can get a reference to the ViewModel in question.
On the ViewModel
public MainViewModel()
{
Current = this;
}
public static MainViewModel Current { get; set; }
Anywhere else in the application:
MainViewModel.Current.DoSomething();
Routed Command
Routed commands are typically databound, and thus the command logic depends on which Data Context it is written on. If Window1's DataContext is MainViewModel, and Window2's DataContext is SecondViewModel, in order to have a button on Window2 execute a command on MainViewModel, you will have to have a reference to that instance of MainViewModel as the DataContext for the button in question.
Background:
I have WPF application with a main window containing a user control. I want to pass a value from the main window to the user control.
In the Main window's constructor I have code:
public MainWindow()
{
InitializeComponent();
_vm = new MainWindowViewModel();
this.DataContext = _vm;
ucControl = new UserControl1("NameSet");
}
(ucControl is my user control)
User control has two constructors:
public UserControl1()
{
InitializeComponent();
this.ID = ID.GetNewID;
}
public UserControl1(string name)
{
InitializeComponent();
_vm = new UCViewModel(name);
this.DataContext = _vm;
this.ID = ID.GetNewID;
}
The problem is: although the second constructor (with parameter) is called, it is not loaded in the main window. I checked the ID (this.ID) in the user control's loaded event and I see the ID set in the default constructor and its DataContext is null. Because of this reason, I do not get the "name" string in my user control.
Any help please? Since I am using MVVM pattern I do not want to expose properties in user control (view) to be set from main window just for this.
You are instantiating the UserControl1 object twice:
Once within the XAML. The <uc:UserControl1> element instantiates a UserControl1 object, using the default constructor, and assigns it to the member ucControl.
You instantiate it again within the constructor of the MainWindow object
If you put a break point in the constructor of UserControl, you'll notice it is called twice. I assume WPF instantiate and initialize the XAML's UserControl (#1 from above) after you assign the dynamic UserControl (#2 from above), and this is why you see the former in the logical tree of MainWindow.
You should have only one instance. If you want to parameterized a user control, the canonical paradigm is what you mention that you don't want to do (why??). If you had such a property, you could set it in the XAML: <uc:UserControl1 x:Name="..." YourProperty="NameSet>
exposing such a property is a single line in the UserControl:
public YourProperty { get; set; }
If you insist of not having this line, you should do the following:
Remove the XAML's user control.
In main window, subscribe to the Loaded event
In the handler of the Loaded event, instantiate a new UserControl1 - with whatever constructor parameter that you want.
Manually add it to the Children array of the parent Grid element
Clearly this isn't my recommendation. In addition to the complexity, with the former method you'll also work very well with the Visual Studio designer.
how to make something like this:(Screen is not from my app)
When is change the button in dialogBox, it automatically change the Bitmap in MainWindow...
I hope you understand my prob ;d
So how can i make this dialog window ?
Or how can i get access to Mainwindow variables from dialogWindow?
If you have no architecture to support that (like MVVM), you can simply use the Application object in you dialog code-behind:
MainWindow mainWindow = Application.Current.MainWindow as MainWindow;
mainWindow.button1.Content = "Dialog rename me";
Application object is set on your application execution and is global.
Define a class where you put all information relevant to your item.
All properties uses NotifyPropertyChanged.
One of this property is the Title.
So now in your main window you have a TextBlock bound your object title (it might be within a control that draws the object and write the title above), and when you click on a button it opens another window. In the constructor( new()) of the second window, you give the drawn object as argument.
So when you change the title in the second window, it gets updated in the first.