我有两个窗口,我想隐藏正在执行第二个窗口打开的窗口。
ViewModel:
public class MainWindowViewModel : INotifyPropertyChanged
{
private bool _isVisible;
public bool isVisible
{
get { return _isVisible; }
set
{
_isVisible = value;
NotifyOfPropertyChange("isVisible");
}
}
public MainWindowViewModel()
{
Window1 X = new Window1();
isVisible = false;
X.Show();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyOfPropertyChange(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
XAML:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"></BooleanToVisibilityConverter>
</Window.Resources>
<Window.Visibility>
<Binding Path="isVisible" Converter="{StaticResource BooleanToVisibilityConverter}" />
</Window.Visibility>
新窗口正在显示,但是应该隐藏的窗口仍然存在,我无法理解为什么会发生。
答案 0 :(得分:0)
要解决您的问题,在绑定Mode=TwoWay
时必须设置Visibility
。
我不知道为什么WPF需要这样的显式声明,但我进行了测试和工作。
<Window.Visibility>
<Binding Path="isVisible" Mode="TwoWay" Converter="{StaticResource BooleanToVisibilityConverter}" />
</Window.Visibility>