WPF, Different UserControl Navigation on the same TabItem - c#

http://s7.directupload.net/images/140511/66z6w2tq.jpg
As you can see on the picture I am explaining everything with a Schema, I have a TabItem and on this TabItem I want to change the DataContext everytime with a Button. What I mean,is that I want to navigate through different UserControls but to stay on the same TabItem from a TabControl. I dont know how to Navigate through different ViewModels and everytime my values not to change from my Elements. I also could not find an Example of the same situation. Any Help with a Code Snippet or a full example will be appriciated.
Thanks in advance.
WPF/MVVM

I think this is a good case for using pub sub with EventAggregator or MvvmLight's Messenger.
You can then loosly coupled notify the MainViewModel to exchange controls or views within a tab.
Basically you can notify another ViewModel with another scope to do certain changes and then subscribe to it.
A possible structure could be:
- A MainViewModel with a collection of TabItemViewModels
- A single TabItemViewModel could contain also Controls and nested ViewModels
If you want to change a tab send a ChangeTabMessage to the MainViewModel
If you want to navigate within a tab use also Messaging or navigate as you do today.
Check out the EventAggregator here: http://msdn.microsoft.com/en-us/library/ff921122.aspx
or check out MVVMligh Toolkit Messenger ...
HTH

Related

WPF MVVM with multiple nested UserControls/Models

For last two years I started diving into programming with C#. After reading and watching tutorials I tried to make my own app but I'm stuck at the point to get everything glued together.
I have a MainWindow with a TabControl. The first tab is an Overview and works fine with my Model and ViewModel (BaseViewModel implemented with RaisePropertyChanged() calls).
The next tabs are more or less independent to eachother but they have multiple UserControls in there.
The UserControls are not only plain collections of TextBoxes and Buttons but also have (editable) ComboBoxes and ListViews which filled up by other Models.
XAML Pseudo-code:
<MainWindow>
<Grid>
<TabControl>
<TabItem1> Overview ViewModel </TabItem1>
<TabItem2> UserControl1 </TabItem2>
<TabItem3> UserControl2 </TabItem3>
... etc
</TabControl>
</Grid>
</MainWindow>
A UserControl can occur at least once or multiple times inside a TabItem (via Button Command, add/remove to/from ObservableCollection). Inside that UserControl a ListView (or another approach) can be filled with data bound to an underlying model.
I already tried to implement my UserControls with registered dependencies but I don't see how I could register a ListView inside the UserControl to be able to bind to it. So I switched to another approach of having a separate ViewModel for each "parent UserControl". At this point I'm also stuck because I cannot be sure the "child UserControls" or models are a subset of the parent UserControl.
So somehow I'm doing something wrong but I don't see what exactly.
I hope someone can provide some hints.
EDIT:
I've put an overview on how my data should be represented.
General Overview for Models in UserControls
Any ideas on how to implement those structures are welcome.

How to use a button while also using a DataModel in WPF

I'm creating an application in WPF using MVVM. I have a tab called tab1, which is a UserControl and has an associated DataModel (but no View, and thus, no ViewModel). Within tab1's content, there is a ListView, inside which is a button. The problem is that I would like the button to work. It worked when tab1 had an associated Tab1ViewModel, but I am told it must have a DataModel and not a ViewModel. I don't believe DataModels can support commands, so does anyone know of a workaround for this? Or does there really just need to be a ViewModel for any nested control with buttons?
Not having a view-model associated with the view is "kind of" against the guidelines of MVVM.
I can think about two solutions :
If you are "allowed", you can inherit the DataModel and add some
"ViewModel functionality" (Like the button's command)
If you are "allowed", have a view-model that contains the data-model (in this method you lose the ability to listen property changes if bind your view to the nested data-model fields)
Add the event handler in view's code-behind (which is against the MVVM's guidelines)
These two solutions aren't that great so I suggest that you try to solve the issue that leads to the facts that you can't use a view-model with your view and solve that.

How to programmatically click a button in WinRT?

How is this possible for WinRT?
I have read the answer for WPF How to programmatically click a button in WPF?, but does not really help me.
Anyone solved such an issue?
First, I'd like to say if you want the button to "appear to have been pressed (in terms of animation/highlight effects) this won't help you but otherwise it should.
My Advice to you would be to follow the Model-View-ViewModel (MVVM) design pattern when designing your application if you haven't already. That way instead of calling the "button" click you can simply execute the method in your viewmodel that would normally be bound to that click.
Example:
You create a model class representing data in your database.
You create a view (page/window) with buttons and other UI elements on it.
You create a ViewModel class that has a series of public methods and collections.
Now in the XAML for the View, you bind the ViewModel as your DataContext and bind the public properties of the ViewModel to your collections (ItemSource for a ListBox being bound to an ObservableCollection is on example). You can create public methods that are "commands" and bind them your buttons so that when the button click event is fired, the command in the view model is executed. Now for all your unit tests and for any other reason you might want to programmatically "click" the button, you can simply call the associated methods in the ViewModel and never worry about what the actual View is doing.

WPF: One window, multiple views

Once I asked how to display multiple views in one window in a windows-forms-application (link).
Now I'd like to know how to do the same in an WPF-application.
You could have in your MainWindow.xaml, just one Stackpanel. Nothing else.
You define all your views in other xaml files. Just that the parent element is not a Window. You need to have it as a Grid/StackPanel.
When you want to load a new view, you just set the appropriate view's root element(or the view itself) as the Children of the StackPanel in MainWindow.xaml
Your best option is to use an MVVM framework such as Caliburn.Micro, which makes view composition very easy. In this case, you would have your shell screen for example, which would be a conductor, and each of your sub screens would just be other view models, which your shell would have references to, and each would become the active item when they needed to be displayed.
I would recommend you to go to MVVM framework . I think you need a MainTabControl which will have child controls. The child controls are not needed to be Windows but UserControls. You can use DataTemplates in wpf to choose the view according to the viewmodel.
Please do let me know if you need more explanation on this.

WPF MVVM UserControl Binding to create a Wizard UI

I m working in a WPF MVVM Project and I am using one Wpf Window. In this window i have a space for one usercontrol and two buttons next and back. When i click the next button I want usercontrol1 to be replaced with usercontrol2. etc.
Sounds like you're trying to create a wizard-style user interface.
This Code Project article may help. And right here is a good place for getting started too.
You can have Wizard user control which is binded to WizardModel, In the Wizard control put ContentPresenter control, and bind it to WizardModel's WizardPage property. And by changing that WizardPage property, you can change wizard page from model.
Hope this helps, here used the same mechanism for changing views

Categories