Additionally, if you haven't downloaded the modified Northwind database or the SharedAssemblies, you will need to download them at the bottom of the Lesson intro page. (Click me)
(Click on the link and the DB is at the bottom of the lesson.)
Please be sure to modify the app.config file to point to the machine you which you setup restore the database too.
All the assemblies used for the demo are contained in the SharedAssemblies folder so you may need to set your reference paths accordingly.
As with all the lessons, I would like to thank the following:
Telerik for making wonderful RAD controls which make our development lives a lot easier. The Demo versions of 2010 Q1 WPF controls were used and can be found at the Telerik website.
http://www.telerik.com/account/free-...l.aspx?pid=601
A big thanks to Sacha Barber for Cinch, NSVMVVM is a slightly modified version of Sacha's Cinch Framework. His DataWrapper related code is used extensively in this Lesson.
Josh Smith for all his work posted on the internet. Finally to the others who have taken the time to post helpful work on the internet.
In this lesson, I’d like to also thank Marlon Grech for his ACB V2 code. http://marlongrech.wordpress.com/200...or-v2-aka-acb/ It gives us the ability to utilize a collection of AttachedProperties and was added to the WPFMvvmPrism framework.
Areas Covered in this Lesson
Grid Row DoubleClick functionality
Grid ContextMenu
The requirements for this lesson
1.When a row in the order grid is double clicked, it fires a command in the OrderViewModel.
2.When a user right clicks on a row in the Order grid, a ContextMenu is display with Add & Edit items.
3.Each item of the ContextMenu will fire a command in the OrderViewModel
In this lesson, we’ll be demonstrating the flexible capabilities of the RadGridView.
First we’ve added code from Marlon Grech which will give us the ability to create a collection of AttachedProperties. The code can be found at:
http://marlongrech.wordpress.com/200...or-v2-aka-acb/
Grid Row DoubleClick functionality
With this new functionality, we can add a collection of command behaviors to a control. For our first requirement, we utilize the RadGridView’s MouseDoubleClick event as follows.
Code:
<WPFMVVMPrism:CommandBehaviorCollection.Behaviors> <WPFMVVMPrism:BehaviorBinding Event="MouseDoubleClick" Command="{Binding Path=EditOrderCommand}" CommandParameter="{Binding ElementName=RGVOrders, Path=CurrentItem}" /> </WPFMVVMPrism:CommandBehaviorCollection.Behaviors>
We created a RelayCommand in the OrderViewModel called EditOrderCommand. We bind to the RadGridView’s CurrentItem which will pass the command a OrderModel object.
That’s all we need to do. Our code now just displays a message box, however you could take the OrderModel and pass it to an OrderEditView to bind to and display in our Popup window for a user to edit.
This satisfies Requirement 1.
Grid ContextMenu
The RadGridView gives us the ability to define a ContextMenu and define MenuItems.
Code:
<telerik:RadGridView.ContextMenu> <ContextMenu x:Name="OrdersContextMenu"> <ContextMenu.Items> <telerik:RadMenuItem Header="Add" Command="{Binding Path=AddOrderCommand}" CommandParameter="{Binding ElementName=RGVOrders, Path=SelectedItem}" /> <telerik:RadMenuItem Header="Edit" Command="{Binding Path=EditOrderCommand}" CommandParameter="{Binding ElementName=RGVOrders, Path=SelectedItem}" /> </ContextMenu.Items> </ContextMenu> </telerik:RadGridView.ContextMenu>
The MenuItem’s allow us to define Commands & CommandParameters. The only gotcha with this code is a ContextMenu isn’t included as part of the VisualTree. So for these bindings to work, we must add the following line of code to the constructor of our OrderView.Xaml.cs file.
Code:
NameScope.SetNameScope(OrdersContextMenu, NameScope.GetNameScope(this));
Also, we need the CurrentItem to get updated when a user right clicks on a row. So we wire up the grids MouseRightButtonDown event.
In the event we do the following to get the currentRow and we set it to selected
Code:
private void RGVOrders_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { RadGridView rgv = (RadGridView)sender; var source = e.OriginalSource as FrameworkElement; object dataContext = null; if (source != null) { dataContext = source.DataContext; rgv.SetIsSelected((object)dataContext, true); } }
Again, for now Messageboxes are displayed. However, you can wire up any type of functionality you wish.
That wraps up Lesson 9. If you have any questions or comments, please let me know







Section Widget
Categories Widget (top-down)

