I'm having trouble with the Commit and Cancel buttons in the dataform for Silverlight. At first I couldn't figure out why the Cancel button was not enabled when the user clicked edit. After some research I found this was because the object was not IEditableObject. That sorted the cancel button but now the Commit button has decided to become enabled, where it wasn't before, even after the value has changed.
My question is, how do I get it is to be enabled?
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
}
I put in the 'OnCustomerPropertyChanged' event handler to see if I could force the dataform to acknoledge the Customer property has changed but it doesn't make a difference, even though the event is firing. I've tried removing the IEditableObject to confirm that this is the problem...
public class VMAccount : VMBase//, IEditableObject
...
Thanks for any help.
EDIT: I should add that Customer is an RIA Entity