getting values from a second window to the main window in wpf - wpf

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

Related

Modify property of MainWindow from UserControl

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
}

Notify button clicked in popup window to main window on wpf

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.

ShowDialog() behind the parent window

I am using ShowDialog() with WindowStyle = WindowStyle.SingleBorderWindow; to open a modal window in my WPF (MVVM) application, but it lets me navigate to parent window using the Windows taskbar (Windows 7).
I've found an answer here: WPF and ShowDialog() but it isn't suitable for me because I don't need an "always on top" tool window.
Thanks in advance
Try setting the Owner property of the dialog. That should work.
Window dialog = new Window();
dialog.Owner = mainWindow;
dialog.ShowDialog();
Edit:
I had a similar problem using this with MVVM. You can solve this by using delegates.
public class MainWindowViewModel
{
public delegate void ShowDialogDelegate(string message);
public ShowDialogDelegate ShowDialogCallback;
public void Action()
{
// here you want to show the dialog
ShowDialogDelegate callback = ShowDialogCallback;
if(callback != null)
{
callback("Message");
}
}
}
public class MainWindow
{
public MainWindow()
{
// initialize the ViewModel
MainWindowViewModel viewModel = new MainWindowViewModel();
viewModel.ShowDialogCallback += ShowDialog;
DataContext = viewModel;
}
private void ShowDialog(string message)
{
// show the dialog
}
}
I had this problem but as the Window was being opened from a view model I didn't have a reference to the current window. To get round it I used this code:
var myWindow = new MyWindowType();
myWindow.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
You can use: myWindow.Owner = Application.Current.MainWindow;
However, this method causes problems if you have three windows open like this:
MainWindow
|
-----> ChildWindow1
|
-----> ChildWindow2
Then setting ChildWindow2.Owner = Application.Current.MainWindow will set the owner of the window to be its grandparent window, not parent window.
When the parent window makes (and shows) the child window, that is where you need to set the owner.
public partial class MainWindow : Window
{
private void openChild()
{
ChildWindow child = new ChildWindow ();
child.Owner = this; // "this" is the parent
child.ShowDialog();
}
}
Aditionally, if you don't want an extra taskbar for all the children... then
<Window x:Class="ChildWindow"
ShowInTaskbar="False" >
</Window>
Much of the reason for the MVVM pattern is so that your interaction logic can be unit tested. For this reason, you should never directly open a window from the ViewModel, or you'll have dialogs popping up in the middle of your unit tests.
Instead, you should raise an event that the View will handle and open a dialog for you. For example, see this article on Interaction Requests: https://msdn.microsoft.com/en-us/library/gg405494(v=pandp.40).aspx#sec12
The problem seems to be related to Window.Owner, and indeed if you judge by previous knowledge that you might have of the Win32 API and WinForms, a missing owner would be the typical cause of such a problem, but as many have pointed out, in the case of WPF that's not it. Microsoft keeps changing things to keep things interesting.
In WPF you can have a dialog with a specific owner and you can still have the dialog appear in the taskbar. Because why not. And that's the default behavior. Because why not. Their rationale is that modal dialogs are not kosher anymore, so you should not be using them; you should be using modeless dialogs, which make sense to show as separate taskbar icons, and in any case the user can then decide whether they want to see different app windows as separate icons, or whether they want to see them grouped.
So, they are trying to enforce this policy with complete disregard to anyone who might want to go against their guidelines and create a modal dialog. So, they force you to explicitly state that you do not want a taskbar icon to appear for your dialog.
To fix this problem, do the following in the constructor of your view class:
ShowInTaskbar = false;
(This may happen right after InitializeComponent();
This is equivalent to Xcalibur37's answer, though the way I figure things, since WPF forces you to have both a .cs file and a .xaml file, you might as well put things that are unlikely to change in the .cs file.
Add "ShowInTaskbar" and set it to false.
Even if this post is a bit old, I hope it is OK that I post my solution.
All the above results are known to me and did not exactly yield the desired result.
I am doing it for the other googlers :)
Lets say f2 is your window that you want to display on top of f1 :
f2.Owner = Window.GetWindow(this);
f2.ShowDialog();
That's it , I promise it will not disappear !
HTH
Guy

Open a window after the mainWindow and activate it

In my application is required the user to select an item from a list before continues uses the application.
To do this, I have a window with the desired items and I display it when the MainWindow displays.
public MainWindow()
{
InitializeComponent();
var itemsWindow = new ItemsWindow();
itemsWindow.Show();
}
The problem is that the window opens in background. How can I open the window in foreground?
The preferable would be to open the itemsWindow on applications start up and onClose event of itemsWindow to display the mainWindow, but I think this approach is far away from my knowledge. Nevertheless, I would appreciate it if someone could post something for how to achieve this.
Thanks
Use ShowDialog() method instead. That way, you won't have to worry about activating that window and user won't be able to interact with MainWindow until ItemsWindow has been closed.
Example:
var itemsWindow = new ItemsWindow();
itemsWindow.ShowDialog();

How can I access one window's control (richtextbox) from another window in wpf?

I'm sure this is something very simple but I can't figure it out. I've searched here and on msdn and have been unable to find the answer. I need to be able to set the richtextboxes selection via richtextbox.Selection.Select(TextPointer1, Textpointer2).
Application.Current contains a collection of all windows in you application, you can get the other window with a query such as
var window2 = Application.Current.Windows
.Cast<Window>()
.FirstOrDefault(window => window is Window2) as Window2;
and then you can reference the control from your code, as in
var richText = window2.MyRichTextBox
Application.Current.Windows.OfType(Of MainWindow).First
You should be able to access controls on Window1 from Window2 code behind, if that's what you want. Generated fields are internal by default.
All you need is to name the control on Window1, like this:
<RichTextBox x:Name="richtextbox" ... />
In Window2 code behind:
var window = new Window1(); // or use the existing instance of Window1
window.richtextbox.Selection.Select(TextPointer1, Textpointer2);
A better option would be to encapsulate select operation in a method in code behind of Window1, to avoid giving away internal. Then you would have:
// Window1.cs
public void Select(int param1, int param2)
{
richtextbox.Selection.Select(param1, param2);
}
// Window2.cs
var window = new Window1(); // or use the existing instance of Window1
window.Select(TextPointer1, Textpointer2);
You cant access the texbox from another window as it is private to that window you can however work around this by exposing the RichTextBox as a public property on your window (hack)
public RichTextBox RichTextBox {
get{
//the RichTextBox would have a property x:Name="richTextbox" in the xaml
return richTextBox;
}
}

Resources