You can find your product's model number on a plate affixed to the product or in the owner's manual. Even though parts may look the same, there are often variances in similar parts and it is important to buy parts that fit your specific model.
Edit: Although this question has been flagged up by @AbinMathew as a possible duplicate of this, the solution provided to that question didn't explain very well how to relay the command logic. I was able to resolve this with the help of John Smith's article as mentioned in my answer.
I've got this test project which I'm running in order to get my head around MVVM. What I'm trying to achieve: MainWindow has a back button and a ContentControl. On Window_loaded I want to display MainGadget in the ContentControl. When I click MyBtn in MainGadget, I want to then display MyGadget in the ContentControl.
ViewModelBase is a class which is used by MainGadgetVM, MainWindowVM and MyGadgetVM. It implements the INotifyPropertyChanged interface. RelayCommand implements the ICommand interface so I want to use it for executing MyBtn_Click and displaying other UserControls.
At the moment, when I run the program only the 'Back' button is displayed. I can't seem to figure out how to display the other UserControls. Any help will be much appreciated.
DataTemplates.xaml
MainWindow.xaml
MainGadget.xaml
MainWindow.xaml.cs
MainWindowVM.cs
4 Answers
Add a binding to your content control and change the bound value to the view model you want to show.
OnurOnurYou can use a style inside your content control that will switch its content based on a common bound property within your main ViewModel.
Something like:
Then, update your ResourceDictionary so that the DateTemplates
have keys:
Now, add the property that is used as the trigger to switch content:
d.moncadad.moncadaAfter trying out the solutions mentioned above, I fell upon John Smith's MSDN article here. In this article he explains how to apply a view to a viewmodel and how to relay the corresponding command logic. Although this was similar to the link posted by @AbinMathew it was more detailed and actually provided the desired solution.
Not the answer you're looking for? Browse other questions tagged c#wpfmvvmbindinginotifypropertychanged or ask your own question.
I have a windows form and i dont want to make any other windows forms just one windows form and different user controls how can i change between user controls for example hide one and show the other user control programmatically ?
i added this but it doesnt work
Eng.Fouad4 Answers
Add a container control (if I remember correctly, there's a containers section in the toolbox?), like a panel. Create usercontrols for what you want to dynamically switch around. So make like a 'HomePage' usercontrol and a 'LoginPage' usercontrol. Dynamically add the usercontrol that you want to display to the container. WHen you want, remove it from the container and add a different usercontrol:
I usually do it this way so you leave your form itself open to add common controls and stuff that might be shared between your different 'pages'.
EDIT: Note that I normally would add the panel in the designer and not create it dynamically in the code. This was just an example.
EDIT: The interaction between your mainform and usercontrols can be handled in a few different ways, and I am not saying that any of these is the correct method.
- You create a static property for your Panel on the Mainform, so thatyou can always access it to swap your controls around.
In this example I'll also add a static method for it
Then inside your usercontrol you call a predefined method, something like:
- Another method is to bind the button click event on your mainforminstead of inside the form.
Like This:
...
Mike C.Mike C.Create a method that returns a UserControl object. Then put conditions in that method as to which control you want to load at a specific condition and then in your main form code.
where 'control' is the returned control from your method.
To remove the existing one you have to loop through the form1.Controls and find out the control and call 'Remove'.
Update:Mike C has a better idea of adding a panel and loading your desired control on the panel as then it's easy to remove your control and you then don't have to loop through the forms controls to find it and then remove it.
you could try this it will definitely help you as it did helped me a lot it short and straight to the point hope that will help