Binding Commands Without Using the DataContext [silverlight+prism] - silverlight

Hello I have a problem with binding commands to button inside datagrid.
Here should be explanation but it doesn't explain everything.
http://msdn.microsoft.com/en-us/library/dd458928.aspx
What should be in classes in namespace Infrastructure?
Can somebody show me a really basic sample of using this?
not just parts of the code...

The Prism Commanding QuickStart - included with the Prism drop should provide the simple code example you are looking for.
All data-bindings go against the current DataContext unless specified otherwise. The DataContext is inherited down the tree of controls unless a control specifically picks a DataContext.
For example your Button might look like this and would look for the SaveCommand on whatever the DataContext has:
<Button Command="{Binding SaveCommand} />
Your button could also look like this if you wanted to bind to a command exposed on your classes code-behind:
<UserControl x:Name="UserControl">
...
<Button Command="{Binding SaveCommand, ElementName=UserControl}"
...
</UserControl>
Using DelegateCommand is just a way of implementing the ICommand you bind to and that should be visibile in the Prism QuickStart.

Related

MVVM WPF - Viewmodel commandbinding

Iam using MVVM model and I have three usercontrols in a WPF Main window and each usercontrol has datacontext set to different viewmodels in the xaml.
The main window is also attached to a different viewmodel in the datacontext. Mainwindow has three buttons and should be bound via command binding, How can we bind the main window buttons to corresponding usercontrol view model Icommand via xaml?
It's difficult to tell if this is a good solution because there are a lot of missing details about your application architecture.
Based on the premise "I have a window that contains 3 user controls and I want buttons on the window to activate commands on the viewmodels of the controls", one solution could be:
<Window>
<UserControl Name="Control1" />
<UserControl Name="Control2" />
<UserControl Name="Control3" />
<Button Command="{Binding ElementName="Control1", Path="DataContext.Cmd"}" />
<Button Command="{Binding ElementName="Control2", Path="DataContext.Cmd"}" />
<Button Command="{Binding ElementName="Control3", Path="DataContext.Cmd"}" />
</Window>
This is a bit subjective, but in my opinion viewmodels should not be serving more then one view.
The MainWindow's viewmodel should be totally independent from the UserControl's models. I would suggest using a publish/subscriber pattern for sending "events" through your application that handle widely used functionality (see for example Event Aggregator).
If you can't access the ViewModels via a direct binding from DataContext of the MainWindow (i.e. if the three ViewModels aren't in the MainWindow's ViewModel), you can just do this :
<Button
Name="Button1"
DataContext="{Binding DataContext, ElementName=UserControl1}"
/>
This should work if I understood well what you're asking.

WPF MVVM - Activate ViewModel-command by use of hot keys from MainWindow

I have an MVVM application in which the MainWindow contains a grid with several views.
In one of the viewmodels there is a command which I can activate by using hot keys in its corresponding view. I can only activate the command when I am placed in the part of the MainWindow that contains the specific view.
The following code works fine, if I only want to be able to activate the command in the specific view:
ComponentView.xaml:
...
<UserControl.InputBindings>
<KeyBinding Gesture="CTRL+U" Command="{Binding Path=UploadCmd}"/>
</UserControl.InputBindings>
</UserControl>
I would like to be able to activate the command by using the hot keys from any part of the MainWindow.
This is my failed attempt to do the keybinding in the MainWindow, so that the command can be activated from anywhere:
MainWindow.xaml:
<Window x:Class="MyProgram.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:MyProgram.View"
xmlns:vm="clr-namespace:MyProgram.ViewModel"
...>
<Grid>
// Grid content
</Grid>
<Window.DataContext>
<vm:ComponentViewModel>
<KeyBinding Gesture="CTRL+U" Command="{Binding Path=UploadCmd}"/>
</vm:ComponentViewModel>
</Window.DataContext>
</Window>
Is it possible to somehow let the application know where the command is directly in the xaml?
Three options I can think of:
You could place your commands in your main window and then use relative source binding from the child views to the command in your main window.
{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type Window}}}
If you are using an mvvm framework, there is often a way to message / communicate between viewmodels. This is usually not the best solution but can be used in the right circumstance.
Use IOC / Dependecy Injection to create a service that is instantiated as a singleton in your viewmodellocator. Then inject this service implementation into the parent and child viewmodels constructors. Then you can call functions/properties from that service from the parent viewmodel that can be accessed by the child viewmodels. I use this for navigation purposes.
What are you trying to do with those commands?
Hope this helps

Dynamically bind Views into a ContainerControl with MVVM

I've been learning the MVVM pattern with Josh Smith's article and I want to create a classic layout with some links to the right (managed with commands) so when I click one I can show my view to the right into a tab control (inside it there is a ContentControl).
This is simple when I use a DataTemplate with the specific View and ViewModel I want to show on screen like this.
<!-- this section into my MainWindow's resources file -->
<DataTemplate xmlns:vm='clr-namespace:WpfFramework.ViewModels'
xmlns:vw='clr-namespace:WpfFramework.Views'
DataType="{x:Type vm:MySpecificViewModel }" >
<vw:MySpecificView />
</DataTemplate>
But, I want something more generic. I mean that my mainWindow should not know a specific View nor a specific ViewModel. It should only know that it binds to some commands and has a tab control which shows "some view". Every sample including Josh Smith's article seems to have limited universe of views and viewmodels, that's great with a sample.
So, how can I tell my ContentControl that some view (with its corresponding viewModel) is gonna be there without being so specific (without "burning" into the mainView the concrete types)?
best regards
Rodrigo
PD. I have tryed with base a ViewModel and Base View but it doesn't seem to work.
In your main View, bind a ContentControl to a generic ViewModelBase property
<ContentControl Content="{Binding CurrentPage}" />
CurrentPage would be defined in the main ViewModel as a ViewModelBase object, and to switch pages you simply set CurrentPage to whatever you want.
So when you click on something like the HomePageCommand, the main ViewModel would execute CurrentPage = new HomePageViewModel(); providing that HomePageViewModel inherits from ViewModelBase.
I wrote something a little while ago that shows some samples here if you're interested

PRISM UserControl and ServiceReference

I'm using MVVM in my project and here is my question. I have a View and corresponding view-model with service reference. This view contains UserControl, which have another UserControl and it also contains nested UserControl. Last UserControl have a method which creates a popup. And in this popup i need service reference from view model. Each user control has own DataContext.
Code explanation.
View xaml:
<UserControl DataContext="{Binding ViewModel}">
<FunctionsList/>
</UserControl>
FunctionsList xaml:
<UserControl>
<Function1/>
<Function2/>
<Function3/>
<Function4/>
</UserControl>
Function3 xaml:
<UserControl/>
Function3 code behind contains CreatePopup method, which creates dialog with a UserControl Function3Popup as Content. And Function3Popup should have Service reference.
What is the best practice here? I have awful solution to pass reference using binding but it seems discouraging to me.
Well, after all I've implemented popup with own ViewModel and resolved it from parent control using command binding in the nested child control. I think it's the best solution here.

MVVM What is the way of updating a UI after a command?

I'm learning MVVM through a project and I got stuck on something simple.
I have a Button that updates a ListView. I have a command in the ViewModel that make the right things but I want to select the new row and get the focus on a TextBox after I click the Button.
The question is: How do I update my UI after executing a command?
If I need to change my windows Title when an operation have been made, I use a property on the ViewModel that is binded to the Window's title and it's changed when I need it but, I don't know how to get focus on a control when a command has been executed.
Thank you.
To select the new row, add a new property to your ViewModel ("SelectedItem" for instance), and bind the ListView's SelectedItem property to it :
<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">...
In the ViewModel, you just have to assign the new item to the SelectedItem property
To focus the TextBox, Mike's idea seems a good one
You could make an attached behavior. I'd suggest using the new Blend behavior framework, ie TriggerAction that contained this custom logic.
For your attached behavior you put on the button, give it a DP for an ICommand and maybe a DP of a ListView type.
On the "protected override void Invoke(object parameter)" of your TriggerAction, execute your ICommand, then you have reference to your ListView. Here you can do your custom code on it, like setting focus.
Your XAML may look something like this:
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Behaviors:CustomBehavior Command="CommandName" ListView="{Binding ElementName=myListView}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Button/>
I suggest looking at Mike Brown's ExecuteCommandAction behavior (download here), it's about almost 1/2 of what you need.
What about setting focus to the control in the code behind: textBox.Focus()
I consider everything you mention in your question to be GUI logic so I would add a Click event to the button to handle stuff that needs to happend in the GUI.
Hope this helps.
I think you need to use the Mediator pattern. Please see this:
Josh Smith's Mediator Prototype for WPF Apps
This is generally used in communicating with the View from the View-Model. Hope this helps.
In your case you need some way that the ViewModel notifies the View that it should set the focus on a specific control.
This could be done with an IView interface. The view implements this interface and the ViewModel can call a method of the View over this interface. This way you have still the View and ViewModel decoupled of each other.
How this can be done is shown here:
WPF Application Framework (WAF)
http://waf.codeplex.com

Resources