I'm very new to WPF. I have to create an WPF app which will do the following:
1) User can change the UI at run time. Controls in each UI are fixed. Suppose two textboxes for numeric value accept and command button to perform multiplication operation and alert the result.
2) Most importantly user can browse the folder of XAML files (UI) and select them / change then at run time. There can be 'n' number of UI users can create and use.
3) I want to use MVVM pattern.
I found this as best example but with doubt. Doubt is that, can user browse user controls and select then, set them? Do they need to be created and added to list before build see this ?
Which is the best way to achieve this task?
If you follow the MVVM pattern, you should create a View (implemented by an User Control) for each UI that your user selects. The Main View will have a ContentControl (or another control type that can host user controls like a HeaderedContentControl) which can host the other Views that the user selects. The view changes will be performed by the associated view model, just like the example you gave.
Trying to answer your question, you should create a menu where the user can select the View to be displayed. The existing views don't need to be added to a list before, because they will already exist in your project. If you want a more detailed example, with code, just update your question with some example you want, like the user can select two views from a menu for instance, and I will add some code .
Related
I've created a simple dialog here with a list of people. All I want to do is a few simple things which are rather trivial in winforms. However I'm struggling to grasp how to do this in wpf with an mvvm approach. Could someone help me out please.
When the user hits the Add New Person button, I want a dialog to appear where a user can type in their Name and either hit OK or Cancel. If the user hits OK, it appends the person to the list. (keep in mind i'll add more edting fields for the users to input, for example last name...)
When the users has a single item in the list selected, i want them to give them the ability to click an 'edit' button showing a dialog similar to the one in the previous step, pre populated with the selected items data, so a user can edit the properties. Then depending on if they hit OK or Cancel the changes are committed.
Dropbox solution: https://www.dropbox.com/s/8sjpabfod08yil5/AddDeleteItems_basic.zip?dl=
To create and display dialogs using MVVM, you'll want to implement a dialog service pattern to handle such requests. This service would be called from within your view model.
There is a really informative Code Project article about this very topic (Showing Dialogs When Using The MVVM Pattern. My advice would be to read that article and then look through the source files accompanying the article. This should provide you with a very good starting point to achieve your use cases (custom dialogs) and can also be used for displaying standard dialogs too (e.g. open file, save file, browse folder etc).
The service pattern isn't the only way to implement this, in the following article I show how to do it using the same data-driven mechanism used for regular windows:
http://www.codeproject.com/Articles/820324/Implementing-Dialog-Boxes-in-MVVM
I suggest this tutorial for you to get started. However, for the dialog Xceed library has some cool custom controls that may help you.
I'm kinda new to the whole windows platform and I'm trying to create mvvm portable class library for windows and windows phone. Now that I have all my smaller pieces ready, I've found myself stuck at trying to figure out how to achieve the following with mvvm.
I have a main page where I can see the information for a car rental reservation and in this page. I'll be able to see a pickup location and a dropoff location (which are both inside a PointOfInterestViewModel). When I click on either the pickup/dropoff location. I want to switch to anther page which has a pivot with multiple list of possible locations. Once the user selects a location, I'll want to update the location in the PointOfInterestViewModel and then send the user back to the main view.
Is there a way where I can achieve it without having multiple PointOfInterestViewModel(s) and passing data between them? I would much rather have one single viewmodel but still adhere to mvvm.
You could put the PointOfInterestViewModel on the App class, bind the main form to it - then you could update it from the pivot:
((App)App.Current).PointOfInterestVM = ....
I am developing a simple WPF application and I can create a single MainWindow with controls, content, etc.
However, I want to split my app. And I have a problem to build an app with welcome screen where user can choose between two modes, as seen below:
After click on Learn button I want to load this XAML:
And after click on Next (from Learn screen) or Recognize (from Welcome screen) I want to load this XAML:
...everthing in the same MainWindow.
I can't even name what I am trying to achieve. Do I want pages? Or views?
How to handle this situation in WPF ?
This you can do it in many ways. Basic thing is place each functionality in different user control, so that you can chose to load them at the run time
Have different user controls and hide all except the first one, depending on the users selections you show the on you want or
Use Content Presenter, depending the user selection load the controls at run time in to the content presenter.
I would like to write a program that uses several tabs, each showing a data grid. The data of all the grids is stored in a single data source. Each tab should look identical except for the number of colums/rows and the values of course.
So I am now trying to put the grid view into a user control and create a .dll. If a tab is added I put the user control into it - this works already. The grid works in virtual mode and if a new tab/grid is created it calls the event to load the data into the grid.
Now I have the problem that the grid and the LoadData event are in the user control while the data source is in my main program. So in the user control the data source is undefined. I thought that this would be ok because you do not run the user control alone and once it is used in the main program the data source should be available to the LoadData event. However, it does not seems to be possible to generate the .dll while the data source is unknown.
On the other hand I do not want to put the data source into the user control because then it is dublicated every time a new tab is created.
I hope you can give me a hint what is the best way to implement it.
thank you very much!
I don't know what answer (how detailed) you expect, but I think this is a very good example to:
reuse one control
use MVVM architecture pattern
I would try to seperate class which would execute logic responsible for creating ViewModel. Control (grid in your purpose) would be always binded to this ViewModel, and this Control would be reuse in different Views. Depend on where it would be placed you can add some additional features if it is necessary (Decorator design pattern). If you expected more detailed (not conceptual) solution than sorry.
All in WPF:
Developing a wizard application, user has to answer a number of simple questions before brought to the main app. The main app is then prefilled with the information obtained from the wizard.
I started with a Window which I then planned to add usercontrols to. The main window would have the user control in the first row, then Next and Previous buttons to control moving between the controls in the second row. This way I could easily control the logic to switch between screens like:
WizardControl1.IsVisible = false;
WizardControl2.IsVisible = true;
But for some reason, user controls do not have setter for IsVisible. Hurray.
So then I thought I would just use seperate windows for each section of the wizard. The problem with this approach is that now when stepping between, the window opens in random positions, and by steppign through the wizard with next, the next window pops up randomly which is really distracting and frustrating.
So how can I develop a wizard properly? I don't get why this is so hard...not exactly rocket science... replacing text and controls and storing input after pressing next/previous!
Thanks
Check this link:
http://www.codeproject.com/KB/WPF/InternationalizedWizard.aspx
This is the article about building wizard in WPF by Josh Smith, it's seems to be nice pattern.
I found it's helpful for me, hope you'll too.
There is also an open source Avalon Wizard control on codeplex.
I'd probably aproach this using data binding and template selectors. Have the wizard form bind to a "WizardData" class, which exposes a list of "WizardPage" base classes.
The WizardData class can expose properties defining the correct info on the forms, and display a control for the main page that uses a template selector to determine the proper control to display based on the actual type of the particular wizard page.
It sounds like more work than it is, really. It also gives you the benefit of good separation between code and UI (all "work" is done by the WizardData and WizardPage classes), and the ability to test logic independent of the UI.
It's also a very WPF/MVVM way of approaching the problem.
I recognize this does not directly address your question, but I thought I'd mention it as a possible alternative. I've used Actipro's Wizard control with pretty good results, and when I have needed support, they have been very responsive. I am not affiliated with them in any way; I just like not having to write the plumbing to manage a wizard.
The property is called "Visibility".
I find that I do better when I dynamically add and removing controls rather than hide them.
I was looking for a Wizard solution too. I have the need to stick with stock WPF components so I implemented the wizard using a standard form and a tab control.
I only hide the tabs at runtime so there available in the IDE. At runtime just use Back, Next, Finish... to navigate thru the tab items
works good