WPF Loading screen for MVVM - c#

I am not sure where to begin with this so any help would be great. My application needs log on/loading screen. The first window will validate the user against a database then load multiple collections. Once done loading the "Log on/loading screen" closes and the actual main window appears. So this is not a true parent child relationship.
The loading of each collection is done ModelView. This is what I am trying to do, in the "Log on/loading screen";
MyViewModel _MyVM = new MyViewModel();
var newWindows = new MainWindow();
newWindows.Show();
this.Close();
The problem is figure out how to call the _MyVM in the MainWindow.
thanks for the help

You could open your LogOn view with the methode ShowDialog before the call to InitializeComponent() on your MainWindow

It seems like you're asking how to give the viewmodel to the window to be its DataContext:
MyViewModel _MyVM = new MyViewModel();
var newWindows = new MainWindow { DataContext = _MyVM };
newWindows.Show();
this.Close();

Related

Passing data between user controls in wpf

I have a user control with a button which when clicked opens a new user control.
private void Button_Click(object sender, RoutedEventArgs e)
{
Window window = new Window
{
Title = "Window2",
Content = new UserDataControl2()
};
window.ShowDialog();
}
I need to pass a collection to the new user control. How can I do it?
The easiest way is to create a custom constructor for your user control.
// Button_Click event
Window window = new Window
{
Title = "Window2",
Content = new UserDataControl2("My Data");
};
// User Control class.
string _info;
public UserDataControl2(string info)
{
_info = info.
};
You could also create a method or property in the user control to receive the data as well. Use whichever seems more appropriate in your context.
The best way is passing object to DataContext of this Window. For this you will need to create a class where store this parameters (ViewModels) and after "binding" to the Window (View). After you can pass this object assigning to Datacontext.
Look to MVVM model to understand better what I mean.
MVVM Pattern Made Simple
MVVM in Depth

Close child window from parent viewmodel wpf mvvm

Close child Window after clicking ok/cancel.
I am invoking the child window from parent mvm model:
//parent view model
var optionSetViewModel = new OptionSetViewModel();
var optionSet = new OptionSet();
optionSet.SetViewModel(optionSetViewModel);
optionSet.SetOwner(_componentview);
optionSet.ShowDialog();
The code for ok and cancel for child Window is written in child view model:
private void OkClick()
{
_childWindow.Close();
}
It closing fine but after closing when it is returing to parent view model from where it is called it is throwing null reference exception. After close I am accesing the child window values:
_someText = optionSetViewModel.SomeText;
_noteText = optionSetViewModel.NoteText;
_optionsetLanguage = optionSetViewModel.OptionSetSelectedItem;
_optionsetselected = optionSetViewModel.OptionSetSelected.ToString();
With Catel (see http://www.catelproject.com/) you can close a view based on a view model. Thus closing a view means a call like this:
myChildViewModel.CloseViewModel();
To show a child window:
var myChildViewModel = new MyChildViewModel();
uiVisualizerService.Show(myChildViewModel);

How to open a dialog center-screen or center-owner?

I have a MVVM application and I have a main viewmodel. In this model I use the following code to open a dialog:
dlgViewModel myDlgViewModel = new dlgViewModel();
dlgView myDlgView = new dlgView();
myDlgView.DataContext = myDlgViewModel;
myDlgView.Owner = App.Current.MainWindow;
myDlgView.WindowStartupLocation = WindowStartupLocation.CenterOwner;
myDlgView.ShowDialog();
Also, in the XAML on my dlgView, I set the WIndowsStartupLocation to CenterOwner. However the window does not open in the center of the owner. I tried CenterScreeen but that does not work either.
How can I open a new view using the centerOwner or centerScreen option?
Well, the first time thing is set in the axml of my dialog WindowsStartUpLocation to center owner.
Then, in the main view model, I use this code:
dlgViewModel myDlgViewModel = new dlgViewModel();
dlgView myDlgView = new dlgView();
myDlgView.DataContext = miDlgViewModel;
myDlgView.Owner = App.Current.MainWindow;
myDlgView.ShowDialog();

Pass value from child window back to MainPage

I have a child window in Silverlight and I wish to send a string value to populate a text box in the applications MainPage.xaml.
How can I pass the value back?
I have tried this -
MainPage m = (MainPage)Application.Current.RootVisual;
m.textBox1.Text = value;
You should do this the other way around. The parent that opens the child window should attach an event handler to an event of the child, for example:
childwindow.ButtonClicked += new EventHandler(childWindow_ButtonClicked);
Within this handler, the Parent can update its own Controls with values from properties of the child Window.
private void childWindow_ButtonClicked(object sender, EventArgs e)
{
txtValue.Text = childwindow.Value;
}
Assuming that you are using the mvvm pattern, you could use the ShowDialog method of the child window to open it.
The ShowDialog method waits until the window is closed.
After the window is closed, you can read the dependent properties from the window viewmodel and set their values in the mainpage.
var view = new ChildWindowView();
var model = new ChildWindowViewModel();
view.DataContext = model;
var result = view.ShowDialog();

How do I programmatically create a windows form?

I have a unique c# source file named source.cs that i compile using CSharpCodeProvider from a builder to get an executable.
I would put an option on the builder whether to display the About form on application startup or not.
How can i create a form with title as About Us then add controls within (Labels, RichTextEdit etc..)
Something like
if (display_about_dialog) {
// code to display the form }
Any help would be highly appreciated
Try something like this:
using (Form form = new Form())
{
form.Text = "About Us";
// form.Controls.Add(...);
form.ShowDialog();
}
Here's the documentation page for the System.Windows.Forms.Form class.
if you have a class MyForm : System.Windows.Forms.Form (that you create using windows form builder)
You can do
MyForm form = new MyForm();
form.Show();
To launch an instance of MyForm.
Though if you want to create a simple confirmation or message dialog, check out the many uses of MessageBox
MessageBox.Show("text");
MessageBox.Show("text", "title", MessageBoxButtons.OKCancel);
Form aForm = new Form();
aForm.Text = #"About Us";
aForm.Controls.Add(new Label() {Text = "Version 5.0"});
aForm.ShowDialog(); // Or just use Show(); if you don't want it to be modal.
Form is a class which you can instantiate like any other, set it's properties, call it's methods.

Categories