如何使用多重绑定绑定开始、结束和持续时间?

How to bind Start, End and Duration with Multibinding?

我的 ViewModel 中有两个属性:

public double Start { get; set; }
public double Duration { get; set; }

我的视图中有三个文本框:

<TextBox Name="StartTextBox" Text="{Binding Start}" />
<TextBox Name="EndTextBox" />
<TextBox Name="DurationTextBox" Text="{Binding Duration} />

我想实现以下行为:

我可以通过监听代码隐藏中的 TextChanged 事件来实现这一点。不过,我更愿意通过 MultiBinding 来实现这一点。可能吗?

我在尝试使用MultiBinding时遇到的问题:

我认为打击代码更容易实现您想要的行为:

XAML:

<TextBox Name="StartTextBox" Text="{Binding Start}" />
<TextBox Name="EndTextBox" Text="{Binding End}" />
<TextBox Name="DurationTextBox" Text="{Binding Duration}" />

视图模型:

    private double _start;
    private double _duration;
    private double _end;
    public double Start
    {
        get
        {
            return _start;
        }
        set
        {
            if (_start != value)
            {
                _start = value;
                Duration = _end - _start;
                OnPropertyChanged("Start");
            }
        }
    }
    public double Duration
    {
        get
        {
            return _duration;
        }
        set
        {
            if (_duration != value)
            {
                _duration = value;
                End = _duration + _start;
                OnPropertyChanged("Duration");
            }
        }
    }
    public double End
    {
        get
        {
            return _end;
        }
        set
        {
            if (_end != value)
            {
                _end = value;
                Duration = _end - _start;
                OnPropertyChanged("End");
            }
        }
    }

ViewModel中的OnPropertyChanged是这样实现INotifyPropertyChanged接口的:

public class ViewModelBase:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}