Silverlight 的数据表单中的提交和取消按钮有问题。起初我不明白为什么当用户点击编辑时取消按钮没有启用。经过一番研究,我发现这是因为该对象不是 IEditableObject。这对取消按钮进行了排序,但现在 Commit 按钮已决定启用,即使在值更改之后,它也不是以前的状态。
我的问题是,我如何才能启用它?
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);
}
}
虚拟机帐户:
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");
}
}
虚拟机基础:
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 实体