4

我是 MVVM 和 Silverlight 的新手,我只是想找出一个简单的场景。

我正在使用没有 Expression Blend 的 MVVM Light 工具包和 Silverlight 3.0。

我有一个 DataGrid 和一个 DataForm 绑定到 ViewModel 中的可观察集合。RelayCommand Save()在对 DataForm 控件中的数据进行更改后,我想绑定到我的属性,并在不使用视图背后的代码的情况下完成此操作。

DataForm 不使用cmd:ButtonBaseExtensions.CommandMVVM Light 用于普通按钮单击命令绑定的那个,所以我不确定如何将控件绑定到我的 ViewModel。

任何帮助表示赞赏!

4

1 回答 1

7

我在发布问题后不久就想通了。去搞清楚。

使用 MVVM Light Toolkit 时,您可以使用该EventToCommand功能绑定到事件。

我的 Xaml 看起来像这样:

<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>

我的 ViewModel 看起来像这样(例如,在 ViewModel 中使用直接启用 silverlight 的 WCF 服务,通常会将其拉入接口以解耦):

//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);
            }
        }
    }
}
于 2010-06-07T15:05:30.403 回答