简体   繁体   English

WPF中的大量显式绑定更新

[英]Mass Explicit Binding Update in WPF

I am writing a WPF application and in it a few models. 我正在编写WPF应用程序,并且其中包含一些模型。 Each model has an editor control with which I can view & edit objects of that model. 每个模型都有一个编辑器控件,通过它可以查看和编辑该模型的对象。

For example: 例如:

        <Label Content="{Binding ID}" />

        <TextBox Text="{Binding FirstName}" />
        <TextBox Text="{Binding LastName}" />
        <TextBox Text="{Binding Department}" />
        <TextBox Text="{Binding TelephoneNumber}" />
        <xceed:ByteUpDown Value="{Binding AccessLevel1}" Maximum="64" />
        <xceed:ByteUpDown Value="{Binding AccessLevel2}" Maximum="64"/>
        <xceed:IntegerUpDown Value="{Binding PIN}" />
        <xceed:IntegerUpDown Value="{Binding KeyCode}" />
        <xceed:IntegerUpDown Value="{Binding UserLimit}" />

Whenever I change a value it is updated in the model, which is great; 每当我更改值时,它都会在模型中更新,这很棒。 but I also want too add a save/cancel behaviour: 但我也想添加一个保存/取消行为:

On "Save", and only then, the data will be copied to the model, and on "Cancel", the data will be reloaded from the model. 只有在“保存”时,才将数据复制到模型,而在“取消”时,将从模型中重新加载数据。

I am not sure how to accomplish it. 我不确定如何做到这一点。

One way I thought of is marking all bindings w/ UpdateSourceTrigger=Explicit , but it requires a large amount of boilerplate code to update the source and it can get a bit cumbersome, since some models have more than 20 editable properties. 我想到的一种方式是使用UpdateSourceTrigger=Explicit标记所有绑定,但是它需要大量的样板代码来更新源,并且由于某些模型具有20多个可编辑的属性,因此可能会有些麻烦。

Is there a better way of doing so? 有没有更好的方法呢?

Edit: I thought maybe someone reading this in the future would like to have the solution I used. 编辑:我想也许将来有人读过这篇文章,希望得到我使用的解决方案。

Given class Key : 给定class Key

class Key
{
    private KeyViewModel viewModel;

    public KeyViewModel ViewModel
    {
        get
        {
            if (viewModel == null)
                viewModel = new KeyViewModel(this);
            return viewModel;
        }
    }
    // -=-=- Lots & lots of properties -=-=- //
}

public class KeyViewModel : Key
{
    public Key Parent { get; set; }
    public KeyViewModel(Key parent)
    {
        Parent = parent;
        CopyFromModel();
    }
    public void CopyToModel()
    {
        Type t = typeof(Key);
        var props = t.GetProperties();
        foreach (var p in props)
        {                
            if (p.CanWrite)
                p.SetValue(Parent, p.GetValue(this));
        }
    }
    public void CopyFromModel()
    {
        Type t = typeof(Key);
        var props = t.GetProperties();
        foreach (var p in props)
        {
            if (p.CanWrite)
                p.SetValue(this, p.GetValue(Parent));
        }
    }
}

直到“保存”之前不更新Model (在ViewModel级别保留值),然后在“取消”时将Model中的值重新加载到ViewModel怎么办?

One other way of achieving this can be to keep and expose the temporary Model at your VM and bind UI with that. 实现此目的的另一种方法可以是在您的VM上保留并公开临时模型,然后将UI与该模型绑定。

Once user click Save, update the Actual Model with your Temporary Model values. 用户单击保存后,使用您的临时模型值更新实际模型。 If user clicks Cancel, just clear the temporary model values. 如果用户单击“取消”,则只需清除临时模型值。 Keep updating the temporary model with the actual selected model values. 继续使用实际选择的模型值更新临时模型。 So your temporary model will act as a intermediate container of values. 因此,您的临时模型将充当值的中间容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM