Changing Content of Window from Button Click in User Control - c#

I am working on a program that has a main window with only a ContentControl element.
In the class for this main window, I can change the content by ContentHolder.Content = new UserControlMain(). In UserControlMain, I have a button to once again change the content of the window to a new user control.
I am running into issues with changing the window content the second time. If I do not instantiate a class of MainWindow, I cannot access the Content property from anywhere except its own class. I also cannot access the ContentControl element I have associated with that class. However, if I do instantiate a MainWindow object, I run into an error message stating that the content has already been set.
Any tips on how I may get around this?

My advice is:
In MainWindow create a Grid with a name
<Grid name = "mainGrid">
</Grid>
Then, in code behind you can do easily this:
mainGrid.Children.Clear();
mainGrid.Children.Add(new UserControlMain());
Regards,

Related

Making a new instance of user control every time I want to change content of wpf window?

I've been looking around and have found that the way to set the Content of a WPF Window is to set the Content of the window to a new instance of a UserControl, for example: MyWindow.Content = new UserControl1();
If I understand this correctly, it creates a new instance of the UserControl. Well what about when you want to go back to that previous UserControl, like if you have a back button on another UserControl? (Hopefully that made sense) Wouldn't that take up unnecessary space in memory? What would be the proper way to do this?
I think what you can do is to save the usercontrol to memory, let's say the dictionary
public Dictionary<string, UserControl> dicForUserControl = new Dictionary<string, UserControl>();
Whenever you wnat to go back to the previous usercontrol, you can reset the MyWindow.Content again.
I have the similar issue before. The way I do is to use the tab control, every tab item is a usercontrol, then you can go back to any one of them by clicking the tab.

MahApps Metro DialogCoordinator: Display Dialog to span UserControl only (instead of entire window)?

I'm epxloring different ways to best show dialog windows in my application.
MahApp Metro's IDialogCoordinator seems quite useful, but I couldn't quite adjust it to my use case yet.
Say I'm creating a UserControl (view), whose ViewModel needs to be able to display dialogues.
These dialogues should, when displayed, overlay/span the UserControl only, NOT the entire Window in which the UserControl is hosted.
Is there any way to achieve this?
Default behavior always seems to span over the entire window, and I haven't found any way to change this yet.
So far, I've been using the Dialog coordinator in a very straightforward way, doing the following in my view:
<UserControl
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}">
and set set the instance in my view's constructor by,
viewModel.Initialize(DialogCoordinator.Instance);
which I'd then call in the viewmodel via
IDialogCoordinator _DialogCoordinator; // set with viewModel.Initialize() called from the view
private async Task _SomeCmdExecute()
{
await _DialogCoordinator.ShowMessageAsync(this, "HEADER", "TEST");
}
Thanks!
Dialogs in MahApps.Metro are always at the window level (see the container PART_MetroActiveDialogContainer in the window's style.)
What you can do is changing the styling of dialogs, so they don't stretch horizontally accross the entire window. See the default template MetroDialogTemplate for reference.

DevExpress. DockLayoutManager. DocumentPanel adding. How to get a link to the original window?

I use component DevExpress DockLayoutManager
According to the documentation on their website
http://documentation.devexpress.com/#WPF/DevExpressXpfDockingLayoutPanel_Uritopic
Control property "returns the root element of the Window / Page" - ie Grid.
Example is in their demo application: DockingDemo.Wpf DocumentGroups.xaml
And here is my code
DocumentGroup documentContainer = dockManager.GetItem("documentContainer") as DocumentGroup;
DocumentPanel panel = dockManager.DockController.AddDocumentPanel(documentContainer,new Uri("/WpfSample;component/MyWindows/Win1.xaml", UriKind.Relative));
panel.Caption = "SomeName";
MyWindow win = ((panel.Control as Grid).Parent as MyWindow);
win.DoInit(object Obj);
And when I call (panel.Control as Grid). Parent - I get a link to DockLayoutManager.
I do not quite understand. Window goes where? DockLayoutManager becomes Parent in the logical tree for the Grid of the Windows?
Tell me please - how to get Window, cast it to the required class and call its method?
ie how to make this code work
MyWindow win = ((panel.Control as Grid).Parent as MyWindow);
win.DoInit(object Obj);
According to the documentation, when the URI refers to a Window, the AddDocumentPanel method only loads the specified Window content. The Window object itself, its resources and event handlers are not loaded, and they cannot be accessed via the LayoutPanel.Control property.
You can use a UserControl object instead of the Window one. In this case, the UserControl itself will be loaded and you will be able to access the loaded UserControl via the LayoutPanel.Control property.

WPF window content as content of another window

i need to show my window as inside Tab of another window and i do it using the following
var childWindowEditor = new MainWindow(); //window to embed
object content = childWindowEditor.Content; //take out the content of the window to embed
childWindowEditor.Content = null;
EditorTab.Content = content; //just a tab in a tabbed group
its working fine, but the problem is what if i wanted to access the functions of the window class?
i know tha i should create it as user control, but is there a solution without having to create the content of that window as a user control?
would casting the content to the window that i got the content from work?
You should use the MVVM pattern. Your ViewModel would be the DataContext of childWindowEditor. So you could simply do something like
EditorTab.DataContext = childWindowEditor.DataContext;
And that's it. I really recommend you to use MVVM. It makes your life much easier.
You may use Pages and frames to display pages.
Add the frame on tab like this:
<TabItem>
<Frame Name="Frame1"></Frame>
</TabItem>
Show page inside frame:
Frame1.Content = new MyPage1();

WPF: adding a usercontrol to a window

First off, I'm new to WPF and C# so maybe the issue I have is really easy to fix. But I'm kinda stuck at the moment.
Let me explain my problem.
I have a WPF Window and two usercontrols (Controls and ContentDisplayer).
The usercontrol Controls, wich contains some buttons, is added in the XAML of the Window.
Nothing special here.
Window.XAML
<nv:Controls/>
Now, what I want to do is when a user is pressing a button in Controls, ContentDisplayer needs to be added to the Scatterview I have in my Window.
I solved the problem by adding the buttons to the Window, and not using the usercontrol Controls. But this is not what I want.
Window.XAML.CS
private static void Button_ContactChanged(object sender, ContactEventArgs e)
{
object ob = Application.LoadComponent(new Uri(
"NVApril;component\\XAML\\ContentDisplayer.xaml",
System.UriKind.RelativeOrAbsolute));
//Set a unique name to the UserControl
string name = String.Format("userControl{0}",
SurfaceWindow1_Scatterview.Items.Count);
UserControl userControl = ob as UserControl;
userControl.Name = name;
//Add the new control to the Scatterview
SurfaceWindow1_Scatterview.Items.Add(userControl);
SurfaceWindow1_Scatterview.RegisterName(name, userControl);
}
So the real question is: How do I add a usercontrol to the Window by pressing a button in an other usercontrol?
Thanks,
Toner
At the top of the window xaml add
xmlns:d="clr-namespace:SomeNamespace.Usercontrols"
where you these exist already, you can choose the namespace of your control from the intellesence list.
Then where you want to place the control type:
<d:yourusercontrolhere params />
and your usercontrols can be added there.
Within Controls expose an event that is fired when you want to add a new control.
public event EventHandler AddControl;
private void RaiseAddControl()
{
if (AddControl!= null)
{
AddControl(this, EventArgs.Empty);
}
}
Now sink that event in your Window
yourControl.AddControl += ContactChanged
In your window, it sounds like you need to add the event to the instances of Controls.
<local:ContentDisplayer>
...
<nv:Controls AddControl="ContactChanged"/>
...
Then in your ContactChanged event handler you can instantiate a new Controls control and add it to whatever collection you're using like in your Button_ContactChanged event handler above.
Let me know if you need further clarification.
I have no idea what you are trying to do your example,
So you have a control defined thus:
public partial class somecontrolname : UserControl
With your corresponding Xaml file
All you need to do to add it in code to your window is firstly you need a LayoutRoot such as Grid control in the window then just
[mylayoutcontrol].Children.Add(new somecontrolname());
Maybe I got wrong idea what you are trying to do, your example code doesn't make much sense to me, looks like you are trying to load the xaml source file

Categories