C# 在WPF MVVM中,我是否需要将要从模型绑定的所有属性包装为依赖属性?

C# 在WPF MVVM中,我是否需要将要从模型绑定的所有属性包装为依赖属性?,c#,wpf,mvvm,C#,Wpf,Mvvm,我正在编写一个具有大规模数据模型的应用程序——作为其中的一部分,我第一次使用MVVM模式。有许多屏幕用于管理各种实体,因此有许多视图模型。我发现每个视图模型都将我正在处理的POCO实体的每个属性包装在依赖项属性中,这样我就可以将它绑定到编辑器字段,然后在用户提交更改时将其写回实体。这对我来说是一个巨大的额外的腿部工作,我不禁想知道我是否错过了重点,或者是否有更简单的方法来实现我的目标。例如,我有一个地址视图模型: public class AddressViewModel : EntityVie

我正在编写一个具有大规模数据模型的应用程序——作为其中的一部分,我第一次使用MVVM模式。有许多屏幕用于管理各种实体,因此有许多视图模型。我发现每个视图模型都将我正在处理的POCO实体的每个属性包装在依赖项属性中,这样我就可以将它绑定到编辑器字段,然后在用户提交更改时将其写回实体。这对我来说是一个巨大的额外的腿部工作,我不禁想知道我是否错过了重点,或者是否有更简单的方法来实现我的目标。例如,我有一个地址视图模型:

public class AddressViewModel : EntityViewModel<Address>
{
    #region Properties

    public string AddressLine1
    {
        get { return (string) GetValue(AddressLine1Property); }
        set { SetValue(AddressLine1Property, value); }
    }

    // Using a DependencyProperty as the backing store for AddressLine1.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AddressLine1Property =
        DependencyProperty.Register("AddressLine1", typeof (string), typeof (AddressViewModel), new PropertyMetadata(string.Empty, HandleAddressChange));

    private static void HandleAddressChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var vm = d as AddressViewModel;
        if (vm != null)
        {
            vm.OnPropertyChanged(AddressAsSingleLineStringPropertyName);
        }
    }

    public string AddressLine2
    {
        get { return (string) GetValue(AddressLine2Property); }
        set { SetValue(AddressLine2Property, value); }
    }

    // Using a DependencyProperty as the backing store for AddressLine2.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AddressLine2Property =
        DependencyProperty.Register("AddressLine2", typeof (string), typeof (AddressViewModel), new PropertyMetadata(string.Empty));

    public string AddressLine3
    {
        get { return (string) GetValue(AddressLine3Property); }
        set { SetValue(AddressLine3Property, value); }
    }

    // Using a DependencyProperty as the backing store for AddressLine2.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AddressLine3Property =
        DependencyProperty.Register("AddressLine3", typeof (string), typeof (AddressViewModel), new PropertyMetadata(string.Empty));

    public string AddressLine4
    {
        get { return (string) GetValue(AddressLine4Property); }
        set { SetValue(AddressLine4Property, value); }
    }

    // Using a DependencyProperty as the backing store for AddressLine2.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AddressLine4Property =
        DependencyProperty.Register("AddressLine4", typeof (string), typeof (AddressViewModel), new PropertyMetadata(string.Empty));

    public string AddressLine5
    {
        get { return (string) GetValue(AddressLine5Property); }
        set { SetValue(AddressLine5Property, value); }
    }

    // Using a DependencyProperty as the backing store for AddressLine2.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AddressLine5Property =
        DependencyProperty.Register("AddressLine5", typeof (string), typeof (AddressViewModel), new PropertyMetadata(string.Empty));

    public string PostCode
    {
        get { return (string) GetValue(PostCodeProperty); }
        set { SetValue(PostCodeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for PostCode.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PostCodeProperty =
        DependencyProperty.Register("PostCode", typeof (string), typeof (AddressViewModel), new PropertyMetadata(string.Empty, HandleAddressChange));

    /// <summary>
    ///   Gets a value indicating whether this instance is valid for save.
    /// </summary>
    /// <value> <c>true</c> if this instance is valid for save; otherwise, <c>false</c> . </value>
    /// <exception cref="System.NotImplementedException"></exception>
    public override bool IsValidForSave
    {
        get { return !string.IsNullOrWhiteSpace(AddressLine1); }
    }


    /// <summary>
    ///   Gets a value indicating whether this instance is valid for edit.
    /// </summary>
    /// <value> <c>true</c> if this instance is valid for edit; otherwise, <c>false</c> . </value>
    public override bool IsValidForEdit
    {
        get { return true; }
    }



    #endregion

    #region Constructor

    /// <summary>
    /// Initializes a new instance of the <see cref="AddressViewModel" /> class.
    /// </summary>
    public AddressViewModel(Address address) : base(address)
    {
    }

    #endregion

    #region Private Methods

    /// <summary>
    ///   Sets the properties from entity.
    /// </summary>
    public override void SetPropertiesFromEntity()
    {
        AddressLine1 = Entity.AddressLine1;
        AddressLine2 = Entity.AddressLine2;
        AddressLine3 = Entity.AddressLine3;
        AddressLine4 = Entity.AddressLine4;
        AddressLine5 = Entity.AddressLine5;
        PostCode = Entity.PostCode;
    }

    /// <summary>
    ///   Sets the entity from properties.
    /// </summary>
    public override void SetEntityFromProperties()
    {
        Entity.AddressLine1 = AddressLine1;
        Entity.AddressLine2 = AddressLine2;
        Entity.AddressLine3 = AddressLine3;
        Entity.AddressLine4 = AddressLine4;
        Entity.AddressLine5 = AddressLine5;
        Entity.PostCode = PostCode;
    }
}
公共类AddressViewModel:EntityViewModel
{
#区域属性
公共字符串地址行1
{
获取{return(string)GetValue(AddressLine1Property);}
设置{SetValue(AddressLine1Property,value);}
}
//使用DependencyProperty作为AddressLine1的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性AddressLine1属性=
DependencyProperty.Register(“AddressLine1”、typeof(string)、typeof(AddressViewModel)、new PropertyMetadata(string.Empty、HandleAddressChange));
私有静态void HandleAddressChange(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var vm=d作为AddressViewModel;
if(vm!=null)
{
vm.OnPropertyChanged(AddressAsSingleLineStringPropertyName);
}
}
公共字符串地址行2
{
获取{return(string)GetValue(AddressLine2Property);}
set{SetValue(AddressLine2Property,value);}
}
//使用DependencyProperty作为AddressLine2的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性AddressLine2Property=
DependencyProperty.Register(“AddressLine2”、typeof(string)、typeof(AddressViewModel)、new PropertyMetadata(string.Empty));
公共字符串地址行3
{
获取{return(string)GetValue(AddressLine3Property);}
设置{SetValue(AddressLine3Property,value);}
}
//使用DependencyProperty作为AddressLine2的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性AddressLine3Property=
DependencyProperty.Register(“AddressLine3”、typeof(string)、typeof(AddressViewModel)、new PropertyMetadata(string.Empty));
公共字符串地址行4
{
获取{return(string)GetValue(AddressLine4Property);}
set{SetValue(AddressLine4Property,value);}
}
//使用DependencyProperty作为AddressLine2的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性AddressLine4属性=
DependencyProperty.Register(“AddressLine4”、typeof(string)、typeof(AddressViewModel)、new PropertyMetadata(string.Empty));
公共字符串地址行5
{
获取{return(string)GetValue(AddressLine5Property);}
设置{SetValue(AddressLine5Property,value);}
}
//使用DependencyProperty作为AddressLine2的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性AddressLine5属性=
DependencyProperty.Register(“AddressLine5”、typeof(string)、typeof(AddressViewModel)、new PropertyMetadata(string.Empty));
公共字符串邮政编码
{
获取{return(string)GetValue(PostCodeProperty);}
set{SetValue(PostCodeProperty,value);}
}
//使用DependencyProperty作为邮政编码的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty PostCodeProperty=
DependencyProperty.Register(“邮政编码”、typeof(字符串)、typeof(AddressViewModel)、new PropertyMetadata(string.Empty、HandleAddressChange));
/// 
///获取一个值,该值指示此实例是否对保存有效。
/// 
///如果此实例对保存有效,则为true;否则为false。
/// 
公共覆盖布尔值为有效值,用于保存
{
获取{return!string.IsNullOrWhiteSpace(AddressLine1);}
}
/// 
///获取一个值,该值指示此实例是否对编辑有效。
/// 
///如果此实例对编辑有效,则为true;否则为false。
公共覆盖布尔值为有效预编辑
{
获取{return true;}
}
#端区
#区域构造函数
/// 
///初始化类的新实例。
/// 
公共地址视图模型(地址):基(地址)
{
}
#端区
#区域私有方法
/// 
///从实体设置属性。
/// 
public override void SetPropertiesFromEntity()
{
AddressLine1=实体。AddressLine1;
AddressLine2=实体。AddressLine2;
AddressLine3=实体。AddressLine3;
AddressLine4=实体。AddressLine4;
AddressLine5=实体。AddressLine5;
邮政编码=实体。邮政编码;
}
/// 
///根据属性设置实体。
/// 
public override void SetEntityFromProperties()
{
Entity.AddressLine1=AddressLine1;
Entity.AddressLine2=AddressLine2;
Entity.AddressLine3=AddressLine3;
Entity.AddressLine4=AddressLine4;
Entity.AddressLine5=AddressLine5;
Entity.PostCode=邮政编码;
}
}
这是一个简单的五属性实体的包装

与MVC web应用程序的腿部工作相比,我只是为模型生成了一个编辑器,我们在开销上有一个非常严重的差异,这样我就可以双向绑定一堆文本框。我非常愿意有人告诉我,我没有抓住重点,而且我做得完全错了,但就我所能做到的而言
"WINDOWPLACEMENT Placement": { for: "Window",
    default: "WINDOWPLACEMENT.Invalid", changed: 1,
    flags: "BindsTwoWayByDefault" }