数据形式提交和取消按钮

时间:2011-01-06 15:19:40

标签: c# silverlight dataform

我在Silverlight的数据表单中遇到提交和取消按钮时遇到问题。 起初我无法弄清楚当用户点击编辑时为什么没有启用取消按钮。经过一些研究后我发现这是因为该对象不是IEditableObject。这对取消按钮进行了排序,但现在提交按钮已经决定启用,即使在值发生变化之后,它仍然没有被启用。

我的问题是,如何启用它?

XAML:

    <dataFormToolkit:DataForm CurrentItem="{Binding ViewModel, ElementName=AccountPage, Mode=TwoWay}" CommandButtonsVisibility="{Binding ViewModel.CommandButtonsVisibility, ElementName=AccountPage, Mode=TwoWay}" AutoEdit="False" AutoGenerateFields="False" AutoCommit="False">
        <dataFormToolkit:DataForm.EditTemplate>
            <DataTemplate>
                <StackPanel>
                    <dataFormToolkit:DataField Label="Organisation Name">
                        <TextBox Text="{Binding Customer.Name, Mode=TwoWay}"/>
                    </dataFormToolkit:DataField>
                </StackPanel>
            </DataTemplate>
        </dataFormToolkit:DataForm.EditTemplate>
    </dataFormToolkit:DataForm>

XAML.cs:

public partial class Account : Page
{
    public VMAccount ViewModel { get; set; }

    public Account()
    {
        InitializeComponent();
    }

    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        ViewModel = new VMAccount(Global.Client.CurrentPerson.Customer);
    }

}

VMAccount:

public class VMAccount : VMBase, IEditableObject
{
    public VMAccount(Customer customer)
    {
        Customer = customer;
    }

    private Customer m_oCustomer;
    public Customer Customer
    {
        get { return m_oCustomer; }
        set
        {
            if (m_oCustomer != value)
            {
                m_oCustomer = value;
                OnPropertyChanged("Customer");
            }
        }
    }

    public event EventHandler<AsyncResultArgs> SaveCustomerSuccess;
    public event EventHandler<AsyncResultArgs> SaveCustomerFailure;

    #region IEditableObject Members

    public void BeginEdit()
    {
        Customer.PropertyChanged += new PropertyChangedEventHandler(OnCustomerPropertyChanged);
        Customer.ContactInfo.PropertyChanged += new PropertyChangedEventHandler(OnCustomerPropertyChanged);
    }

    public void CancelEdit()
    {
        (Customer as IRevertibleChangeTracking).RejectChanges();
        (Customer.ContactInfo as IRevertibleChangeTracking).RejectChanges();
    }

    public void EndEdit()
    {
        if (Customer.HasChanges)
        {
            Global.Client.MainContext.SubmitChanges((lo) =>
            {
                HandleResult("Save Customer", lo, true, SaveCustomerSuccess, SaveCustomerFailure);
            }, null);
        }
    }

    #endregion

    private void OnCustomerPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnPropertyChanged("Customer");
    }
}

VMBase:

public class VMBase : INotifyPropertyChanged
{
    protected virtual void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null) 
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

我放入'OnCustomerPropertyChanged'事件处理程序,看看我是否可以强制数据表来确认Customer属性已更改,但它没有任何区别,即使事件正在触发。我已经尝试删除IEditableObject以确认这是问题...

public class VMAccount : VMBase//, IEditableObject
...

感谢您的帮助。

编辑:我应该补充一点,客户是RIA实体

2 个答案:

答案 0 :(得分:0)

因此事实证明我正在尝试编辑嵌套对象,在RIA工具包SP1发布之前无法完成。感谢。

答案 1 :(得分:0)

在问题Silverlight 3 Dataform Commit Button not activating

中,您的问题可能类似

请修改this response

我使用silverlight 4和RIA serivces也有同样的想法。

  

我决定安装WCF RIA   服务包1和   重新安装WCF RIA服务   WCF RIA Services SP1的工具包。

     

这两个安装程序可以在以下位置找到:   http://www.silverlight.net/getstarted/riaservices/

     

直接链接到WCF RIA Services SP 1:   http://go.microsoft.com/fwlink/?LinkId=205085   直接链接到WCF RIA服务   WCF RIA Services SP1的工具包:   http://go.microsoft.com/fwlink/?LinkID=205088