Abstract user control - c#

I am fairly new to WPF MVVM.
I have a user control named MyButtonsView which inherits from UserControl. This user control should display two other user controls.
I have few viewmodel classes:
MyRedButtonViewModel, MyBlueButtonViewModel.
They both derive from an abstract class named MyAbsButtonViewModel. This abstract view model enforces all derive view models to implement a property name GetImage.
In the XAML of MyButtonsView, I have a grid that should have two rows. In the first row I want to have MyRedButtonViewModel, in the second row I want to have MyBlueButtonViewModel. Both should display an image inside a button which is binded to GetImage property. (It is polymorphic).
How can I tell the XAML to load the right viewmodel for each row?
is there a proper way to implement it. I am mind opened to change my design.

You can define use different ViewModel as DataContext for the two buttons.
MyButtonsView binding to MyButtonsViewModel, MyButtonsViewModel has two properties MyBlueButtonViewModel and MyRedButtonViewModel.
In the MyButtonsView you can define somethings like this:
<MyRedButton DataContext = "MyRedButtonViewModel" ..../>
<MyBlueButton DataContext = "MyBlueButtonViewModel" .../>

Related

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 bind a grid to a user control

I want to create a settings menu that looks similar to VLC's advanced settings menu: Treeview on the left and some kind of control collection on the right. The controls on the right should enable the user to manipulate settings that are relevant to the current selection in the tree view. I thought about creating a grid right of the tree view. Then I have a user control for each view that needs to be displayed in the grid, based on the selection
The item in the treeview has a UserControl property that holds a reference to the relevant view. My viewmodel has a SelectedItem property that indicates which item in the tree view is currently selected.
Now I want to bind the content of the grid to the UserControl property of my SelectedItem. But I cannot figure out how to do that. I would prefer to use a XAML based solution instead of clearing the Children property of the grid and adding the user control that I want to display in code each time the SelectedItem property changes.
I'd suggest using ContentControl instead of Grid.
Considering the tree view and the content control are under the same view model: on your view model, add property for selected item (let's call it VMSelectedItem) of same type as the items in the tree view.
In XAML of the tree view add
SelectedItem="{Binding VMSelectedItem}"
In XAML of the content control
Content="{Binding VMSelectedItem.UserControl}"
Now selection in the tree will update the VMSelectedItem property that, in turn, will update the content of the content control.
I suggest you using DataTemplates that you have declared inside your resouces dictionay. You would be using only one instance of each DataTemplate which leaves nice memory footprints. You wouldnt need to store instance of view inside your viewmodel which is the basic idea of mvvm. The viewmodel would completely just holding data and information how you wish the data to be displayed.
As example you have an enum inside your viewmodel with values person, car, tree. Inside your DataTemplateSelector you will have an if on that enum that returns what the desired DataTemplate.
Basically you would have everything central instead of having everything per each TreeViewItem.

Datacontext of nested user controls

Background :-
I have a wpf view containing a combobox which gets populated by the view model using caliburn micro / ninject and mvvm pattern; this view also contains a stackpanel area. When the user selects the appropriate option from the combobox I insert the appropriate user control into the stackpanel presenting the user with a seemless transition to the related display.
Each "nested" user control which gets displayed in the stackpanel has it's own view model automatically associated by caliburn micro.
Problem :-
The "nested" user control bindings all try and refer back to the parent view model and not the view model associated with them specifically.
I can, initially, work around this by specifying :-
<UserControl.DataContext>
<vm:UserControlSpecificViewModel/>
</UserControl.DataContext>
but this requires a parameterless constructor in the view model but I need to be able to have paremeters passed to this view model so that Ninject can inject objects such as EventAggregator.
Going around in ciricles as I am fairly new to WPF so any help would be appreciated.
Thanks.
James.
One way to solve your problem could be to just initialize your view model in code behind and call the appropriate constructor. If you have a dislike of code behind in your WPF applications then I suppose you could possibly just bind your view model to the IEventAggregator object.
XAML:
<UserControl1 x:Name="myUserControl">
</UserControl>
Codebehind:
public MainWindow() // Constructor for window
{
InitializeContext();
MyViewModel vm = new MyViewModel(...);
myUserControl.DataContext = vm;
}

binding usercontrol

How can I create a binding to a property defined in an UserControl from XAML which includes it? For example, I have a control of type local:Photo named thePhoto, which has three controls local:Layer called Main, Frame and Text, and I want to access thePhoto.Main.ActualWidth from my MainWindow? Thanks
(I forgot to say that simple Binding with Path and ElementName doesn't work)
Create DependencyProperty of desired type within "parent" UserControl and bind it with needed Properties on both sides.
Nice example of doing stuff like that:
Exploring the use of Dependency Properties in User Controls

How to create WPF UserControl in MVVM

I have one doubt in MVVM, What is the procedure to create UserControl Example EmunRadioButton.
Public class EmunRadioButton: RadioButton.
{
//code
}
My Question is: where to create this EmunRadioButton class that is ( View , ViewModel or Model);
Please give the hint
I dont think this is UserControl. More like CustomControl.
In UserControl, its basicaly multiple controls put together. In CustomControl, you either create or modify completly new control.
In both cases, it would be little wrong to try to put MVVM into this. Especialy because you need to use Dependency properties to expose state of your new control. This way you can use MVVM when you put your control in another View.
Since this class is derived from a View related class, and doesn't touch your model directly I personally would put the file (XAML or code file) in my View folder.

Categories