I have one window and some userControls. When Window loaded, one of UserControls programicaly Hosted in Window.Content. Suppose, userControls have buttons that when clicked by user this Current UserControl Removed from window.Content and destoryed and another userControl added to parent Window.Content. I used Parent like this:
(Parent as Window).Content = new MyUserControl;
But after once changing parent userControl, result of Parent return null and previous userConrol did not destroyed.
Just created the attached sample to test what you are trying to do. Kindly provide some more detail about the code you have. Attached sample does exactly what you said without any null references.
Sample
Related
I am new to Wpf. One UserControl (Wpf UserControl) is added to
the DockPanel and in some action O replaced the first UserControl
with another UserControl in DockPanel dynamically. But the first
UserContorl remains visible and the second UserControl in not showing
in DockPanel.
I don't know the correct behavior of how I can be
loaded dynamically. I added this code (this code is executed well)
but it doesn't reflect the action to display "CtlAddEmployee"
control in Dockpanel.
CtlAddEmployee frm2 = new CtlAddEmployee(str);
DockPanelInRibbon.Children.Clear();
DockPanelInRibbon.Children.Add(frm2);
For full details how it is working is asked in my previous question
in detailed way:
Loading another userControl in wpf Ribbon Window when the we click on DataGrid row
Please provide the solution for this.
yaa here i need to refer old object but not the new created object for the corresponding class reference...
For that better to use parameterized constructors..
I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?
I have a window called MAINWINDOW that has a toolbar and a frame. Inside the frame is shown a Page called HOMEPAGE. This page has a treeview where menu options are shown. When the user clicks a node, the corresponding form (a Page) is shown inside a border in HOMEPAGE, using the Border.Content property. I have a button in the toolbar to search for customers and I need to bind this button's command property to a command which is defined inside the Customers page's viewmodel. This page is a child of the HOMEPAGE which in turn is a child of MAINWINDOW. Can you suggest a way to do this, that is to access a command defined in a child's child object?
Thank you.
That goes against the principles of MVVM.
ViewModels (and their corresponding Commands) are 1:1 to Views. Each ViewModel serves one View, and one View ONLY. It helps to think of the ViewModel as the codeBehind of the View, only that it can never access the View directly (thus it's completely decoupled from it).
Violating this principle (V to VM == 1:1), creates dependencies between ViewModels or between Views, which is a bad thing. It makes your application harder to maintain since each "module" is dependent on the implementation of other "modules".
What you should do is expose another Command on the ViewModel that serves the relevant View. That Command in turn could trigger what ever you want.
I have a WPF app with a Window (RootWindow) with a Toolbar and a Frame (ContentFrame). Initially the Toolbar is hidden.
I load a Login UserControl into the Frame and when the user correctly logs in I'd like to close the UserControl and then make the Parent Window toolbar visible.
Seems such a simple thing to do.
However, you cannot close a UserControl from within the UserControl. So how do I break out of the UserControl so I can remove it from the RootWindow (ContentFrame.Source=Nothing) and also make the toolbar Visible.
I can get a handle for the Parent Window with the following code but I cannot access the controls within it
Dim parentWindow As Window = Window.GetWindow(Me) 'Get a handle for parent window
Ideally, I'd like to be able to access Parent Window Controls from within a Child UserControl or at least be able to Trigger an event in the Parent Window from the Child UserControl.
To find the parent in the hirearchy you can use this code:
http://www.hardcodet.net/2009/03/detecting-double-click-events-on-the-wpf-datagrid
Although the problem solved in above article is for DataGrid, the code to find parent is generic enough and should work in your case.
I wrote a custom Silverlight 3 control that uses a class as its data context (MVVM pattern). I want to place this control on another control (form) through XAML. The child control exposes a Dependency Property that when set through XAML, will make it show detailed info.
So an example is that the child control shows order details data, and I want to place it on a form that show user orders. When you select an order, the selected item value on the parent control (orders list), is data bound to the child control, to show details.
The problem is that the child control’s dependency property's OnChanged handler never gets called. If I do not set a data context on the child (so it uses the parent's data context) all works fine, but when I set a different data context, it breaks down.
Ideally, your ViewModel would be for the outer UserControl and a property on the ViewModel will be the DataContext of the Inner/Child userControl
Its true that when the Parent control's DataContext is set, it is propogated down to all its child controls. But the child control has an option of overriding this behavior by setting its own DataContext (which you seem to be doing in your example). Hence by the rule of preferences, the child control's DataContext is given more preference and thus it overrides the parent's one. Also since the child's DataContext never changes after it's initially set, the DP never gets invoked.
So I thought about this some more, and I understand what is happening, but I think its very confusing, and is not done right. If I am doing data binding on a control in the main page, it should use the context of that page to do the binding. And binding I do inside the control should use the control's context.
The way it works now uses the control's context no matter where I put the binding expression (unless I am doing E2E binding, then its using the main page's context). That is silly to me. But at least I understand it now.
I solved the problem using Element to Element binding, and got it to work. I hope the SL team would change this behavior.