Starting Code in C# in the main window after clicking a button?
Hi,
when I use
EditWin EditForm = new EditWin();
EditForm.Show();
it open a new window an the code of the window executes
but I would like to run the WPF Code Xaml/cs in the same (MainWindow)
Any help how to do it?
Thank yoou in advance
11 Replies
I'd give this a read: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/control-authoring-overview?view=netframeworkdesktop-4.8
I'm making an assumption, but based on your class names your edit 'form' is actually a window. You likely want to make a
UserControl
that you can include in your main window's xaml.Control Authoring Overview - WPF .NET Framework
The extensibility of Windows Presentation Foundation controls minimizes the need to create custom controls. Learn how to create a new control, if necessary.
I read it but I did not see the point where to start - yes I have a user control in my main window - a button e.g
private void btnEditor_Click(object sender, RoutedEventArgs e)
{
EditWin EditForm = new EditWin();
EditForm.Show();
}
and it opens a another form (window) now I would like to see the same in the main window
A
Button
and a UserControl
are two different things. UserControl
is a base you can inherit from you make your own custom controls. I'd give the link above a closer read because it feels as though you missed the point.
For example, you could take all of the xaml from your EditWin
and use it to create a new user control named EditForm
. This new control could then be used wherever you want by doing something like this in your xaml:
What exactly do you mean by 'see the same in the main window'?I have a main window with button and I click on it - but the table with data opens not in a second window - it opens in the same window - when I click back it should come to the first main xaml (window) again - all in the same window
how to achieve this
Not super familiar with WPF (usually mess with Avalonia), but assuming you're following MVVM patterns you'd probably want to have some state on the main windows view model to track which view is being displayed.
From there you have multiple options. You could explicitly tie the
Visibility
property of your views to the state of your main window's view model.
Another option would be using a DataTrigger
to modify the style of your components (which also includes visibility). This stack thread has an example: https://softwareengineering.stackexchange.com/questions/360492/mvvm-correct-way-to-switch-between-views-occupying-the-same-footprint-on-the-scrSoftware Engineering Stack Exchange
MVVM Correct way to switch between views occupying the same footpri...
An example is switching the main view according to the selected item in a hamburger menu. Also sometimes it is just a small part of the GUI that changes according to the application state or accord...
arg Viewnodel etc. hope there is a easier solution 😣
That solution is pretty easy.
I found some user controls tutorials - I think I start there - thank you
or maybe tricking with show hide close window 😁
I'd recommend getting used to the MVVM pattern. May as well pick up useful design concepts as you learn WPF.
Use navigation with pages
https://github.com/SirRufo/so-77657479-PageNavigation
GitHub
GitHub - SirRufo/so-77657479-PageNavigation: Example for stackoverflow
Example for stackoverflow. Contribute to SirRufo/so-77657479-PageNavigation development by creating an account on GitHub.