数据表单提交和取消按钮
我在使用 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 实体
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以事实证明我正在尝试编辑一个嵌套对象,但在 RIA 工具包 SP1 发布之前无法完成此操作。谢谢。
So it turns out I was trying to edit a nested object which can't be done until RIA toolkit SP1 is released. Thanks.
您的问题可能与问题Silverlight 3 Dataform Commit Button not activate类似
请修改此回复
我也遇到了同样的问题使用 silverlight 4 和 RIA 服务。
You problem can be similar at question Silverlight 3 Dataform Commit Button not activating
Please, revise this response
I had the same isue using silverlight 4 and RIA serivces.