【问题标题】:Updating several properties when another property changes?当另一个属性更改时更新几个属性?
【发布时间】:2017-04-24 01:53:44
【问题描述】:

在我的模型类中,我有几个列表和其他属性。其中一个属性称为CurrentIteration,并且会不断更新。当它被更新时,我希望其他属性将自己更新为相应列表的元素,即CurrentIteration 的索引。我认为我需要包含的只是一个OnPropertyChanged 事件,用于我想在CurrentIteration 的设置器中更新的属性。但是,他们似乎没有被调用。

public class VehicleModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private List<double> _nowTime = new List<double>();
    public List<double> NowTime
    {
        get { return this._nowTime; }
        set { this._nowTime = value; OnPropertyChanged("Nowtime"); }
    }

    private List<double> _VehLat = new List<double>();
    public List<double> VehLat
    {
        get { return this._VehLat; }
        set { this._VehLat = value; OnPropertyChanged("VehLat"); }
    }

    private List<double> _VehLong = new List<double>();
    public List<double> VehLong
    {
        get { return _VehLong; }
        set { _VehLong = value; OnPropertyChanged("VehLong"); }
    }

    //non-list properties
    private int _currentIteration;
    public int CurrentIteration //used to hold current index of the list of data fields
    {
        get { return _currentIteration; }
        set
        {
            _currentIteration = value;
            OnPropertyChanged("CurrentIteration");
            OnPropertyChanged("CurrentVehLat");
            OnPropertyChanged("CurrentVehLong");
        }
    }

    private double _currentVehLat;
    public double CurrentVehLat
    {
        get { return _currentVehLat; }
        set { _currentVehLat = VehLat[CurrentIteration]; OnPropertyChanged("CurrentVehLat"); }
    }

    private double _currentVehLong;
    public double CurrentVehLong
    {
        get { return _currentVehLong; }
        set { _currentVehLong = VehLong[CurrentIteration]; OnPropertyChanged("CurrentVehLong"); }
    }

    public void SetData(int i)
    {
        CurrentIteration = i;
    }

    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

CurrentIteration 确实得到了正确更新,但其余的则没有。二传手被完全跳过。我几乎可以肯定这很简单,在这种情况下我对二传手的理解是错误的,但我不确定它到底是什么。

编辑:这是 XAML 中的一种绑定示例:

Text="{Binding Path=CurrentVehLong,
               Mode=TwoWay,
               UpdateSourceTrigger=PropertyChanged}"

【问题讨论】:

  • 您好,您是否介意展示一下 xaml 中的数据绑定对于那些非更新属性如何? - 谢谢
  • 好的,将它添加到帖子的末尾。
  • 一种选择是简单地将OnPropertyChanged("CurrentVehLat"); OnPropertyChanged("CurrentVehLong"); 中的CurrentIteration 设置器替换为对其他设置器的调用(它们将调用它们自己的 OnPropertyChanged):CurrentVehLat = double.MaxValue;CurrentVehLong = double.MinValue;(值不'没关系,你从来没有在那些 setter 中读过value)。通过这种方式,任何时候 CurrentIteration 被分配,他的 setter 都会调用另一个属性的 setter 来获取更新的CurrentIteration 值。

标签: c# wpf mvvm properties


【解决方案1】:

提出属性更改通知只是说“这些属性已更改,请重新查询它们的值”。这意味着它调用了get 访问器方法。

您似乎希望它调用set 方法,这不会发生,因为您没有设置值。

尝试删除支持属性并直接访问数组值,如下所示:

public double CurrentVehLong
{
    get { return VehLong[CurrentIteration];; }
    set 
    { 
        VehLong[CurrentIteration] = value; 
        OnPropertyChanged("CurrentVehLong"); 
    }
}

现在当您调用OnPropertyChanged("CurrentVehLong") 时,它将重新查询get 访问器以获取此属性,并根据CurrentIteration 更新值。我还更改了 set 方法,使其更有意义,如果您想在其他地方执行此操作,您可以使用它来设置值。

【讨论】:

  • 效果很好!我认为我对这些的理解有点肤浅,我的印象是我总是需要一个支持值。那么它们的意义何在? (如果答案很长,请不要担心)。
  • @pfinferno 它只是一种存储数据以供内部访问的方法,因此您不必通过常规的 get/set 访问器方法。这样做的最常见原因是避免任何类型的无限循环,例如this answer shows 显示。您还可以使用自动属性,例如{ get; set; },这将导致编译器为您的内部生成支持字段,尽管在 WPF 的情况下,您通常希望在 setter 中使用 PropertyChange 通知,所以自己写出来。跨度>
猜你喜欢
  • 2020-02-21
  • 2019-07-08
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
相关资源
最近更新 更多