views:

852

answers:

1

I'm new to MVVM and Silverlight and I'm just trying to figure out a simple scenario.

I'm 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 would like to bind to my RelayCommand Save() property after I make changes to the data in the DataForm control and have this accomplished without using the code behind for my view.

The DataForm doesn't use the cmd:ButtonBaseExtensions.Command that MVVM Light uses for normal button click command binding, so I'm not sure how to tie the control to my ViewModel.

Any help is appreciated!

+2  A: 

I figured it out shortly after posting the question. Go figure.

When using the MVVM Light Toolkit you can bind to events using the EventToCommand feature.

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}}">
<Grid x:Name="LayoutRoot" ShowGridLines="False">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <data:DataGrid x:Name="dgAllBusinesses" CanUserSortColumns="True" 
                   IsReadOnly="True" AutoGenerateColumns="True" 
                   ItemsSource="{Binding Businesses}" 
                   Grid.Column="0">
    </data:DataGrid>
    <ScrollViewer x:Name="svScroll" Grid.Column="1" >
        <dataFormToolkit:DataForm x:Name="dfDetails"
                                  ItemsSource="{Binding Businesses}"
                                  AutoGenerateFields="True" 
                                  CommitButtonContent="Save" 
                                  CommandButtonsVisibility="Edit, Navigation, Commit, Cancel" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="EditEnded">
                    <cmdextras:EventToCommand Command="{Binding SaveBusiness}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </dataFormToolkit:DataForm>
    </ScrollViewer>
</Grid>

And my ViewModel looks like this (using direct silverlight enabled WCF service in ViewModel for quick example, would normally pull this 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<GetBusinessesCompletedEventArgs>(_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, GetBusinessesCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                Businesses = new PagedCollectionView(e.Result);
            }
        }
    }
}
hotbot86