简体   繁体   English

WPF 使用 MVVM 的父事件处理程序

[英]WPF Parent event handler using MVVM

It seems that I would like to the opposite of the question posed here .看来我想与这里提出的问题相反。 I have a WPF application that uses the MVVM pattern.我有一个使用 MVVM 模式的 WPF 应用程序。 The main view determines the view to be displayed with主视图确定要显示的视图

<!-- Content -->
<ContentControl Grid.Row="1" Content="{Binding CurrentView, Mode=OneWay}" />

Where CurrentView is the "current" view. CurrentView 是“当前”视图。 In this same main view model I also have buttons that are enabled an disabled via a bind to "IsValid" on the view model在同一个主视图 model 我也有通过绑定到视图 model 上的“IsValid”启用和禁用的按钮

<Button Content="{x:Static r:Resources.Next}" Background="LawnGreen"
                Command="{Binding NextCommand,Mode=OneWay}"
                IsEnabled="{Binding Path=IsValid,Mode=TwoWay}"
                Visibility="{Binding Path=IsNextVisible,Converter={StaticResource BoolToVis}}"/>

This has a simple implementation like:这有一个简单的实现,如:

public bool IsValid
{
    get
    {
        return CurrentView.IsValid;
    }
    set
    {
        RaisePropertyChanged();
    }
}

But you will notice that the "IsValid" flag comes from whatever is the CurrentView.但是您会注意到“IsValid”标志来自于 CurrentView。 Thus each view determines if it is valid or not.因此,每个视图都确定它是否有效。 In one of these views (UserControls) I have a ComboBox that when something is selected the "IsValid" for that view should go from false to true. In one of these views (UserControls) I have a ComboBox that when something is selected the "IsValid" for that view should go from false to true. This looks like:这看起来像:

private Client selectedItem;
public Client SelectedItem
{
    get { return selectedItem; }
    set
    {
        selectedItem = value;
        IsValid = true;
        ProvisionService.SelectedClient = selectedItem;
        RaisePropertyChanged();
        RaisePropertyChanged("IsValid");
    }
}

private bool _isValid;
public bool IsValid
{
    get
    {
        return _isValid;
    }
    set
    {
        _isValid = value;
        RaisePropertyChanged();
    }
}

The problem that I am seeing is that parent doesn't seem to be seeing the event that has changed from false to true in the child view.我看到的问题是父母似乎没有看到在子视图中从假变为真的事件。 How do I notify the parent that this property in the child has changed?我如何通知父母孩子的这个属性发生了变化?

Here is the implementation of CurrentView这是 CurrentView 的实现

private IProvisionView _currentView;
public IProvisionView CurrentView
{
    get { return _currentView; }
    set
    {
        _currentView = value;
        RaisePropertyChanged();
        RaisePropertyChanged(nameof(IsValid));
        RaisePropertyChanged(nameof(IsPrevVisible));
        RaisePropertyChanged(nameof(IsNextVisible));
        RaisePropertyChanged(nameof(IsHomeVisible));
    }
}

You can do the same what WPF does.您可以执行与 WPF 相同的操作。 Assign to the PropertyChanged event.分配给 PropertyChanged 事件。 If your CurrentView is set:如果您的 CurrentView 已设置:

// old (if assigned)
_currentView.PropertyChanged -= CurrentView_PropertyChanged;
// new
value.PropertyChanged += CurrentView_PropertyChanged;

and then check if it was the IsValid property然后检查它是否是 IsValid 属性

Whenever the CurrentView property is set to a new value in the parent view model, you should hook up an event handler to the child view model's PropertyChanged event and handle it by raising the PropertyChanged event for the IsValid property of the parent view model that the Button is bound to, eg:每当CurrentView属性在父视图 model 中设置为新值时,您应该将事件处理程序连接到子视图模型的PropertyChanged事件并通过引发父视图 model 的IsValid属性的PropertyChanged事件来处理它,即Button必然,例如:

private INotifyPropertyChanged _currentView;
public INotifyPropertyChanged CurrentView
{
    get { return _currentView; }
    set
    {
        if (_currentView != null)
            _currentView.PropertyChanged -= OncurrentViewPropertyChanged;
        _currentView = value;
        if (_currentView != null)
            _currentView.PropertyChanged += OncurrentViewPropertyChanged;
    }
}

private void OncurrentViewPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "IsValid") // = nameof(ChildViewModel.IsValid)
        RaisePropertyChanged("IsValid"); // = nameof(IsValid)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM