I currently have one window designed in WPF and coded in C#. I want one of my buttons to open another window, which I would also like to design in WPF. What is the best way for me to do this? Can I make multiple xaml files and call them from the same .cs class? Or should I just have one xaml file? I tried to add a new window into my xaml but it won't allow me to do that. I want all the code to be in the same C# class.
Yes, you can have multiple XAML files and call them from the same .cs file.
For exemple, let's say you have Window1.xaml and Window2.xaml. Window1 is your main window, and the code behind will look like this :
public partial class Window1 : Window
{
public MainWindow()
{
InitializeComponent();
}
}
In Window1 you have a button named btnOpenWindow. On click, you may do that to open Window2 :
private void btnOpenWindow_Click(object sender, RoutedEventArgs e)
{
var window = new Window2();
window.Show();
}
Then a new Window2 is opened.
However you won't be able to get events or others things coming from Window2 in Window1.xaml.cs, obviously you will control that in Window2.xaml.cs for exemple.
You should use the MVVM pattern in your project.
So you have different windows and just one ViewModel to handel these views and your data.
Have a look on: MVVM: Tutorial from start to finish?
Related
I was working on a project using WPF, I want to modify the MainWindow to have some extended window styles. I have referred some docs and found out that I can set the window styles using SetWindowLong(), but in my case it will not work because I am trying to use WS_EX_NOREDIRECTIONBITMAP as the extended window style. when I use SetWindowLong() it does nothing. I have read somewhere that WS_EX_NOREDIRECTIONBITMAP can only be set while creating the window. So is there any way that i could modify the CreateWindowEx() of the WPF MainWindow.
I have found another alternate method to achieve this but it is by using the Undocumented API function.(SetWindowCompositionAttribute()). I need my project to be stable so is there any other method to achieve this?
If SetWindowCompositionAttribute() can set WS_EX_NOREDIRECTIONBITMAP at runtime, there must be a workaround to do this.
Here is the screenshot of what i want to achieve :
It is possible to do it in C++ but in C# WPF i haven't found a way to do this.
If you want to set WS_EX_NOREDIRECTIONBITMAP during creating MainWindow then you can create MainWindow manually. Just remove StartupUri from App.xaml and create new event handler for Startup event.
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Startup">
App.xaml.cs
void App_Startup(object sender, StartupEventArgs e)
{
// replace with your code
MainWindow window = new MainWindow();
window.Show();
}
But it would be useful if you can shore more code.
So I'm still learning how to do C# applications and I have actually a problem with a window.
I've created a WPF project and I've separated some part of my main window in sub-parts (user-controls) so I can have a cleaner xaml code to work with.
I have a lot of different UserControl such as UserControlMenuStrip. All of them are inside the MainWindow.
Inside the MenuStrip was a MenuItem called Parameters :
<MenuItem Header="_Parameters" x:Name="MenuParameters"/>
I have created a new window called ParametersWindow. My goal was to open a child window centered with the main window when I click on the item.
But I don't really know how to proceed? Should I make a click= event and write down the code inside the linked UserControlMenuItem.xaml.cs linked file? Or in the MainWindow.xaml.cs file? Or maybe a new and clean file?
When I try to put it inside UserControlMenuItem.xaml.cs, I can't properly set the owner of the window I create this method but I can't set the owner:
private void OpenParametersWindow()
{
WindowParameters WinParam = new WindowParameters();
WinParam.Owner = MainWindow();
WinParam.WindowStartupLocation = WindowStartupLocation.CenterOwner;
WinParam.Show();
}
And, when I try via the MainWindow.xaml.cs I can't even get the variable...
So... How can I properly open the Window properly? And should I do it in the xaml.cs file or create a new one for a better understanding?
I've Created a class and added a static field as MainWindow to holding reference
class ReferenceClass
{
public static MainWindow mainWindow = null;//firstly null.we will set it in WindowLoaded event.
}
You can create a class like this for accessing reference of your MainWindow from wherever you want.Give your MainWindow reference to its static field.
MainWindow Loaded Event
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ReferenceClass.mainWindow = this; //setting the reference to static field of ReferenceClass.
}
Menu Click (Event called on MenuStrip UserControl)
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
NewWindow nw = new NewWindow();
nw.Owner = ReferenceClass.mainWindow;//Calling the reference of MainWindow from our class.
nw.WindowStartupLocation = WindowStartupLocation.CenterOwner;
nw.Show();
}
Here we go
Project
This is just a way for solving this issue.We can find more solutions which are better than mine but i use this solution when i need.
A handle to the parent window for any usercontrol can be obtained that way:
Window wndParent = System.Windows.Window.GetWindow(this);
WinParam.Show(wndParent);
But when working with WPF, it is more convenient to use the MVVM pattern
As you writing UI in WPF it is preferred to implement it with MVVM pattern. It allows you to have a clear separation of concerns between code and presentation.
Regarding your question about how to set Owner I suggest reading this series of articles about dialogs in WPF implemented with MVVM in mind
https://www.c-sharpcorner.com/article/dialogs-in-wpf-mvvm/
I am trying to learn WPF. I have done mostly back-end programming, except I did some C++ UI programming in the nineties. So far, I have created a simple maintenance application with a few screens and I can run it fine. I can navigate around, insert records and whatnot. However, I have to set my app.xaml startup location to MainWindow.xaml and then instantiate my actual window inside the C# code of the class linked to it. If I delete the MainWindow.xaml file and set my StartupLocation to wndMyMainWindow.cs, I get an error saying that it can not find the file. Is there any way around this? It seems sort of weird to require a non C# file type in what is supposed to be a C# UI framework.
In your App.xaml remove the StartupUri="MainWindow.xaml". Then add Startup="App_OnStartup" and create the matching method in your App.xaml.cs file like:
public partial class App : Application
{
private void App_OnStartup(object sender, StartupEventArgs e)
{
// do some code stuff like initializing your ViewModel or something else
// Instanciate the view you want to display and show it
MainWindow mainWindow = new MainWindow();
mainWindow.ShowDialog();
}
}
I'm accustomed to Winforms where you can create a window and display it via:
Window.ShowDialogue();
I'm using the default MainWindow.xaml in a class library project. I had to delete the App.xaml file to complete the conversion. I want to launch the main window in a simple test. E.G.
[TestMethod]
public void RunPd()
{
MainWindow window = new MainWindow();
window.ShowDialogue();
}
Show/ShowDialogue() is not available. All I have is
GetChildren<>, GetParents<>, InitializeComponent and LoadTree<>.
How can I display MainWindow.xaml?
UPDATE
Main window code:
public partial class MainWindow : Window
Try ShowDialog()
public void RunPd()
{
MainWindow window = new MainWindow();
window.ShowDialog();
}
I'm not sure if this could be the issue or not (your question is a bit vague); however, right-click on the xaml file in the VisualStudio project and click on Properties. Then make sure that "Build Action" is set to "Page."
If this is already set, please make a comment as to that as well.
I had to add a reference to WindowsBase.dll, System.Xaml, PrentationationCore and PrentationFramework. Once I added these references the unit test worked.
Curious as to why I did not need to add these assemblies in Winforms...?
I am learning WPF with M-V-VM. And I am using ICommand, RelayCommand.
I have several Views, Models, and ViewModels.
The MainWIndowView open upon on application start. The MainWindowView has a button that opens another WPF window called “FileListview” via MainWindowViewModel.
The FileListView has a button “View Lookup”, which supposed to open another WPF window called “LookupView” via FileListViewModel. But I could not make this button to work unless I specify FileListView in App.xaml.cs instead of MainWIndowView. I could not understand why “View Lookup” button work if I make application to start from “FileListView” . I also don’t understand whether I need model for MainWindowView, and FileListView since I don’t have anything going except one view’s button is opening another view.
On code behind file “App.xaml.cs” I have
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
WPFProject. MainWIndowView window = new MainWIndowView ();
MainWIndowViewModel VM = new MainWIndowViewModel ();
window.DataContext = VM;
window.Show();
}
}
I would appreciate if somebody can point me to good article or sample code using WPF with M-V-VM that reflect my issue.
Here is my approach to use dialogs/child windows with mvvm and wpf. please note the comment from sllev and post all relevant code.
After rethinking the issue, I was able to figure out the solution.
The cause of the issue: I was not associating View with it’s ViewModel class.
So I put the following code in code behind of FileListView.xaml.cs.
public partial class FileListView: Window
{
private FileListViewModel _ fileListViewModel = new FileListViewModel ();
public FileListViewModel ()
{
InitializeComponent();
base.DataContext = _fileListViewModel;
}
}
Thank you