Silverlight DataForm MVVM Command Bind Update

I am new to MVVM and Silverlight, I just want to figure out a simple scenario.

I am using MVVM Light toolkit and no Expression Blend Silverlight 3.0.

I have a DataGrid and a DataForm bound to an observable collection in ViewModel. I want to bind to my RelayCommand Save() property after changing the data in the DataForm control, and This is done without using the code behind my view.

DataForm does not use MVVM Light for normal button click command binding cmd: ButtonBaseExtensions.Command, so I am not sure how to add The control is bound to my ViewModel.

Any help is appreciated!

I understood soon after posting the question. To figure it out.

When using the MVVM Light Toolkit, you can use the EventToCommand function to bind to events.

My Xaml looks like this:

< UserControl x:Class="CountyBusinessDirectory.UI.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:// schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http:// schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:dataFormToolkit="clr- namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data "
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight"
xmlns:cmdextras="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
DataContext="{Binding BusinessesViewModel, Source={StaticResource Locator}}">



< br />

IsReadOnly="True" AutoGenerateColumns="True"
ItemsSource=" {Binding Businesses}"
Grid.Column="0">


ItemsSource="{Binding Businesses}"
AutoGenerateFields="True"
CommitButtonContent="Save "
CommandButtonsVisibility="Edit, Navigation, Commit, Cancel" >







My ViewModel looks like this (using the Silverlight-enabled WCF service in the ViewModel as a quick example, usually pulled into an interface to decouple):

//using statements ommitted for brevity

namespace MyProject.ViewModels
{
public class BusinessesViewModel: ViewModelBase
{
private PagedCollectionView _businesses;
DALServiceClient _proxy;

public RelayCommand SaveBusiness
{get; private set; }

public PagedCollectionView Businesses
{
get
{
return _businesses;
}
set
{
if (_businesses != value)
{
_businesses = value;

base.RaisePropertyChanged("Businesses");
}
}
}

public BusinessesViewModel()
{
_proxy = new DALServiceClient(); //Data Access Layer WCF Service

_proxy.GetBusinessesCompleted += new EventHandler(_proxy_GetBusinessesCompleted);
_proxy.GetBusinessesAsync();

SaveBusiness = new RelayCommand(() => SaveBusinessToDB());
}

private void SaveBusinessToDB()
{
Business bus = Businesses. CurrentItem as Business;
_proxy.UpdateBusinessesAsync(bus);
}

v oid _proxy_GetBusinessesCompleted(object sender, GetBusinessesCompletedEventArgs e)
{
if (e.Result != null)
{
Businesses = new PagedCollectionView(e.Result);
}
}
}
}

I am new to MVVM and Silverlight, I just want to figure out a simple scenario. < p>

I am using the MVVM Light toolkit and Silverlight 3.0 without Expression Blend.

I have a DataGrid and a DataForm bound to an observable collection in a ViewModel. I want Bind to my RelayCommand Save() property after changing the data in the DataForm control, and do this without using the code behind my view.

DataForm does not use MVVM Light for Normal button click command binding cmd: ButtonBaseExtensions.Command, so I am not sure how to bind the control to my ViewModel.

Any help is appreciated!

I understood soon after posting the question. Go to figure it out.

When using MVVM Light Toolkit, you can Use the EventToCommand function to bind to the event.

My Xaml looks like this:

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System. Windows.Controls.Data.DataForm.Toolkit"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:cmd="clr- namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight"
xmlns:cmdextras="clr-namespace:GalaSoft.MvvmL ight.Command;assembly=GalaSoft.MvvmLight.Extras"
DataContext="{Binding BusinessesViewModel, Source={StaticResource Locator}}">





IsReadOnly="True" AutoGenerateColumns="True"
ItemsSource="{Binding Businesses}"
Grid.Column ="0">


ItemsSource="{Binding Businesses}"
AutoGenerateFields="True"
CommitButtonContent="Save"
CommandButton sVisibility="Edit, Navigation, Commit, Cancel" >







< /Grid>

My ViewModel looks like this (use the Silverlight-enabled WCF service in the ViewModel as a quick example, usually pull it into an interface to decouple):

< p>

//using statements ommitted for brevity

namespace MyProject.ViewModels
{
public class BusinessesViewModel: ViewModelBase
{
private PagedCollectionView _businesses;
DALServiceClient _proxy;

public RelayCommand SaveBusiness
{get; private set; }

public PagedCollectionView Businesses
{
get
{
return _businesses;
}
set
{
if (_businesses != value)
{
_businesses = value;

base.RaisePropertyChanged("Businesses" );
}
}
}

public BusinessesViewModel()
{
_proxy = new DALServiceClient(); //Data Access Layer WCF Service

_proxy.GetBusinessesCompleted += new EventHandler(_proxy_GetBusinessesCompleted);
_proxy.GetBusinessesAsync();

SaveBusiness = new RelayCommand(() = > SaveBusinessToDB());
}

private void SaveBusinessToDB()
{
Business bus = Businesses.CurrentItem as Business;
_proxy.UpdateBusinessesAsync( bus);
}

void _proxy_GetBusinessesCompleted(object sender, GetBusine ssesCompletedEventArgs e)
{
if (e.Result != null)
{
Businesses = new PagedCollectionView(e.Result);
}
}
}
}

Leave a Comment

Your email address will not be published.