1

我有几个罗盘属性,每边一个,一个方向属性。我将方向绑定到组合框,并且在方向的设置器中有一个 case 语句来设置罗盘点。

我遇到的问题是 UI 没有刷新。如果我关闭表单并重新打开它,数据已更改为正确的值,但 UI 不会动态更改。

我需要做什么?

4

1 回答 1

2

对于使用 WPF 的案例,您正在寻找INotifyPropertyChanged允许您将带有属性的消息“发送”到 UI 的界面。

大多数情况下,它位于一个名为 ViewModelBase 的基础中:

public class ViewModelBase : INotifyPropertyChanged
{
    /// <summary>
    /// Raised when a property on this object has a new value
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this ViewModels PropertyChanged event
    /// </summary>
    /// <param name="propertyName">Name of the property that has a new value</param>
    protected void SendPropertyChanged(string propertyName)
    {
        SendPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    /// <summary>
    /// Raises this ViewModels PropertyChanged event
    /// </summary>
    /// <param name="e">Arguments detailing the change</param>
    protected virtual void SendPropertyChanged(PropertyChangedEventArgs e)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    public void SendPropertyChanged<TProperty>(Expression<Func<TProperty>> property)
    {
        var lambda = (LambdaExpression)property;

        MemberExpression memberExpression;
        if (lambda.Body is UnaryExpression)
        {
            var unaryExpression = (UnaryExpression)lambda.Body;
            memberExpression = (MemberExpression)unaryExpression.Operand;
        }
        else
        {
            memberExpression = (MemberExpression)lambda.Body;
        }
        SendPropertyChanged(memberExpression.Member.Name);
    }

    //.Net 4.5? or 4 and BCL included?
    /// <summary>
    /// Raises this ViewModels PropertyChanged event
    /// </summary>
    /// <param name="propertyName">Name of the property that has a new value</param>
    protected void SendPropertyChanged([CallerMemberName]string propertyName = "")
    {
        SendPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }    
}

将 aSendPropertyChanged(() => Property)或 aSendPropertyChanged("Property")写入您的 Setter,UI 将被更新

于 2013-08-05T15:28:33.670 回答