How to add a User Control to a window at runtime?
I need to add a User Control to a window at runtime. Now my problem is that i need to instantiate the window first and then add the User Control to that window. After all this the window only will be shown
开发者_C百科My thoughts was this:
Create the instance of the User Control first:
string managedClassName = "SupplierModule.Views.SupplierInvoiceView, SupplierModule";
var userControl = Activator.CreateInstance(Type.GetType(managedClassName));
Then create the window :
NavigationWindow navigationWindow = new NavigationWindow();
and then add the User Control to the window.
So my problem is, how do i add this user control to the window?
I don't know if i have approached it in the right way. So if its wrong please point me in the right direction.
Thanks
You are using WPF and MVVM
1- Define below code in the Xaml
<ContentControl
Margin="10,0"
Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{Binding TableDetailsDataFormView}"/>
2- Define a TableDetailsDataFormView property in your ViewModel.
3- Assign the property TableDetailsDataFormView when you createded your ViewModel.
4- I am assuming that you have implemented INotifyPropertyChanged then called the appropiate method to notify the UI
If you want to use the NavigationWindow
, you should use a page to which you add the user control. First create a Page
, then set Page.Content
to the user control and then use Navigate(page)
to navigate to this page.
精彩评论