【发布时间】:2021-04-21 03:02:18
【问题描述】:
似乎我想与here提出的问题相反。我有一个使用 MVVM 模式的 WPF 应用程序。主视图决定了要显示的视图
<!-- Content -->
<ContentControl Grid.Row="1" Content="{Binding CurrentView, Mode=OneWay}" />
CurrentView 是“当前”视图。在同一个主视图模型中,我还有通过绑定到视图模型上的“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}}"/>
这有一个简单的实现,例如:
public bool IsValid
{
get
{
return CurrentView.IsValid;
}
set
{
RaisePropertyChanged();
}
}
但是您会注意到“IsValid”标志来自于当前视图。因此,每个视图都确定它是否有效。 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.这看起来像:
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();
}
}
我看到的问题是,父母似乎没有看到在子视图中从 false 变为 true 的事件。如何通知父母孩子的这个属性发生了变化?
这是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));
}
}
【问题讨论】: