3

这个问题已经引起了一段时间的头痛,它阻碍了项目的推进。考虑一个 WPF XAML 表单,其中控件绑定到 ViewModel。(我正在使用 Caliburn.Micro MVVM 框架和实体框架来处理数据)。shell 调用一个Initialize()方法来从数据库加载表单的数据并设置 PropertyChanged 事件处理程序。有一个IsDirty标志可以跟踪表单中是否有更改的数据。有一个绑定到IsDirty属性的“保存”按钮,以便在数据更改时启用它。

// Sample code; forms have many controls....

// this is the property that the controls are bound to
public Entity BoundData { get; set; }

public void Initialize()
{
    // this is an example line where I query the database from the Entity Framework ObjectContext...
    BoundData = objectContext.DataTable.Where(entity => entity.ID == 1).SingleOrDefault();

    // this is to cause the form bindings to retrieve data from the BoundData entity
    NotifyOfPropertyChange("BoundData");

    // wire up the PropertyChanged event handler
    BoundData.PropertyChanged += BoundData_PropertyChanged;

    IsDirty = false;
}

void BoundData_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    IsDirty = true;
}

// implementation of the IsDirty flag
public bool IsDirty
{
    get
    {
        return _isDirty;
    }
    set
    {
        _isDirty = value;
        NotifyOfPropertyChange("IsDirty");
    }
}

问题是由于在方法完成BoundData_PropertyChanged后从数据库初始化表单,导致事件处理程序被命中。Initialize()因此,该IsDirty标志设置为 true,并且启用了“保存”按钮,即使表单刚刚加载并且用户没有更改任何内容。我错过了什么?当然,这是一个常见问题,但我一直无法找到一个好的解决方案。这是我的第一个 MVVM 项目,所以我完全有可能遗漏了一些基本概念。

更新:为了澄清,我认为问题是我需要能够挂钩在所有绑定完成更新时将触发的事件或回调,因此我可以连接 PropertyChanged 事件处理程序。

4

4 回答 4

0

您可以做的一件事可能会有所帮助,那就是设置触发更改的属性,如下所示:

    public virtual bool Prop1
    {
       get
       {
            return _prop1;
       }
       set
       {
            if (_prop1 != value)
            {
                _prop1 = value;
                NotifyOfPropertyChange("IsDirty");
            }
       }

这样,事件只会在值实际更改时触发,而不仅仅是冗余设置。这当然假设在您的情况下该值实际上没有改变。

于 2013-05-23T20:39:49.630 回答
0

我建议你设计一个通用的方法调用。即类似ValueChanged事件处理程序(自定义事件处理程序),它又调用BoundData_PropertyChanged。这意味着,只要控件发生更改,您就可以调用此 ValueChanged 方法/处理程序。

例如

    void ValueChanged(object sender, EventArgs e)
    {
    BoundData_PropertyChanged();
    }

   textBox.TextChanged += new System.EventHandler(ValueChanged);

语法可能不准确,但您可以猜到我在这里提出的建议。

于 2013-05-23T21:36:36.083 回答
0

这个解决方案可能不是万无一失的,但它在我使用的有限测试用例中对我有用。

在第一次输入时,EntityKey 值将为空。检查这个。

/// <summary>
/// Log the invoice status change
/// </summary>
/// <param name="value">The value that the invoice status is changing to</param>
partial void OnInvoiceStatusValueChanging(string value)
{
    var newStatus = ConvertInvoiceStatus(value);
    if (this.EntityKey != null && InvoiceStatus != newStatus)
    {
        AddNewNote(string.Format("Invoice status changing from [{0}] to [{1}]", InvoiceStatus.GetDescription(), newStatus.GetDescription()));
    }
}

/// <summary>
/// Log the invoice status change
/// </summary>
partial void OnInvoiceStatusValueChanged()
{
    if (this.EntityKey != null)
        AddNewNote(string.Format("Invoice status changed to [{0}]", InvoiceStatus.GetDescription()));
}
于 2015-05-16T08:05:52.110 回答
0

我知道这个问题很古老,但是我遇到了完全相同的问题并且很难弄清楚。我对 WPF/MVVM 很陌生,可能不知道谷歌或问的正确事情。这是我的解决方案。希望它可以帮助某人。

我有一个 IsDirty 标志,与原始帖子中的标志几乎相同。唯一的区别是我添加了一个命令来在视图完成渲染时将其重置为 false。基本思想来自于在 View 被渲染/实例化时通知 ViewModel

在视图中触发 Loaded 事件:

<UserControl x:Class="MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding Path=OnLoadedCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

然后在事件触发时在视图模型中将 IsDirty 标志设置为 false。

public ICommand OnLoadedCommand { get; private set; }

// Constructor
public MyUserControlViewModel()
{
    OnLoadedCommand = new DelegateCommand(OnLoaded);
}

public void OnLoaded()
{
  // Ignore any PropertyChanged events that fire 
  // before the UserControl is rendered.  
   IsDirty = false; 
}
于 2016-04-05T14:41:15.503 回答