当 RIA Services DataForm 将更改发送到支持对象时进行更改

发布于 2024-08-05 15:44:47 字数 1828 浏览 5 评论 0原文

一旦用户跳出字段,DataForms 似乎就会更新其 CurrentItem。即使 AutoCommit = false 也会发生这种情况。该行为的副作用是,绑定到数据的其他控件会在用户编辑数据时更新,而不是在用户单击“确定”接受 DataForm 更改时更新。有没有办法修改该行为以推迟将数据写入 CurrentItem 到用户接受更改时?

编辑:这是我正在使用的 DataForm 的大部分 Xaml:

xmlns:DataFormControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"

<DataFormControls:DataForm x:Name="dataForm" AutoCommit="False" AutoEdit="False">           
            <DataFormControls:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">

                        <DataFormControls:DataField Label="Title">
                            <TextBox Text="{Binding Title, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="First Name">
                            <TextBox Text="{Binding FirstName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="Middle Name">
                            <TextBox Text="{Binding MiddleName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="Last Name">
                            <TextBox Text="{Binding LastName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>
                    </StackPanel>
                </DataTemplate>
            </DataFormControls:DataForm.EditTemplate>
        </DataFormControls:DataForm>

编辑 2:我用来避免这种行为的解决方法是复制要编辑的对象并将其设置为 DataForm.CurrentItem 属性,然后如果用户接受编辑,数据将复制回原始对象。我希望有更好的解决方案。

DataForms seem to update their CurrentItem as soon as the user tabs out of a field. This happens even when AutoCommit = false. The side effect of that behavior is that other controls that are bound to the data update while the user edits data instead of when the user clicks Ok to accept the DataForm changes. Is there a way to modify that behavior to postpone writing data to CurrentItem to when the user accepts the changes?

EDIT: Here's most of the Xaml for a DataForm I'm using:

xmlns:DataFormControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"

<DataFormControls:DataForm x:Name="dataForm" AutoCommit="False" AutoEdit="False">           
            <DataFormControls:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">

                        <DataFormControls:DataField Label="Title">
                            <TextBox Text="{Binding Title, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="First Name">
                            <TextBox Text="{Binding FirstName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="Middle Name">
                            <TextBox Text="{Binding MiddleName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="Last Name">
                            <TextBox Text="{Binding LastName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>
                    </StackPanel>
                </DataTemplate>
            </DataFormControls:DataForm.EditTemplate>
        </DataFormControls:DataForm>

EDIT 2: The workaround I'm using to avoid this behavior is to make a copy of the object to be edited and set it to the DataForm.CurrentItem property, then if the user accepts the edit the data is copied back to the original object. I'm hoping there's a better solution out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-08-12 15:44:47

一切都绑定到实体的同一个实例。由于 DataForm 字段绑定到实体上的属性,因此当您离开字段时,它会调用属性设置器,更改值并引发 INotifyPropertyChanged.PropertyChanged 事件。然后,这会通知其他绑定控件该值已更改并且它们的绑定已更新。

如果您确实想防止这种情况发生,则需要做一些繁重的工作来克隆 DataForm 绑定到的实体,然后在提交该项目时,使用克隆的值更新原始项目。不建议这样做。

或者,您可以拥有 DomainContext 的多个实例并加载实体两次 - 一次用于 DataForm,一次用于其他显示。提交对一个实体的更改后,您可以重新加载另一个实体。但这可能会导致您的应用程序出现其他问题,因此也不建议这样做。

我很好奇为什么实时绑定会引起问题。

Everything is bound to the same instance of the entity. Since the DataForm fields are bound to properties on the entity, when you leave a field, it calls the property setter, changing the value and raising INotifyPropertyChanged.PropertyChanged events. This then informs other bound controls that the value has changed and their bindings are updated.

If you really want to prevent this, you would need to do some hefty work to clone the entity that the DataForm is bound to, and then when the item is committed, update the original with the clone's values. This would not be recommended.

Alternatively, you could have multiple instances of the DomainContext and load the entities twice--once for the DataForm and once for the other displays. After submitting changes on one, you could re-load the entities for the other. This will likely lead to other problems in your application though, so it's also not recommended.

I am curious why the live binding is causing a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文