gpt4 book ai didi

c# - 我无法在 MVVM 中隐藏窗口

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:12 25 4
gpt4 key购买 nike

我在 WPF 应用程序中有启动窗口,我的 View 中有以下代码:

     <Window x:Class="MyView"
Name="ucPrincipal"
Title="{Binding Titulo}"
Visibility="{Binding EsUpdaterVisible, Mode=TwoWay}">

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Recursos/Diccionarios/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

<Button Content="Aceptar" HorizontalAlignment="Left" Margin="10,145,0,0" VerticalAlignment="Top" Width="75">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding AceptarCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

我的 View 模型:

private RelayCommand _aceptarCommand;
public RelayCommand AceptarCommand
{
get { return _aceptarCommand ?? (_aceptarCommand = new RelayCommand(aceptarCommand)); }
}


private void aceptarCommand()
{
try
{
EsUpdaterVisible = false;

Titulo = "Después de aceptar.";
}
catch { throw; }
}



private bool _esUpdaterVisible = true;
public bool EsUpdaterVisible
{
get { return _esUpdaterVisible; }
set
{
if (_esUpdaterVisible != value)
{
_esUpdaterVisible = value;
base.RaisePropertyChangedEvent("EsUpdaterVisible");
}
}
}


private string _titulo = "Inicio";
public string Titulo
{
get { return _titulo; }
set
{
if (_titulo != value)
{
_titulo = value;
base.RaisePropertyChangedEvent("Titulo");
}
}
}

当我点击接受按钮时,窗口的标题改变了,但窗口仍然可见。

在某些情况下,我想从 View 模型中隐藏窗口。我该怎么做?

谢谢。

最佳答案

如果您不想使用转换器,只需使用 xaml 部分:

<Window x:Class="MyView"
Name="ucPrincipal"
Title="{Binding Titulo}">
<Window.Style>
<Style TargetType="Window">
<Style.Triggers>
<DataTrigger Binding="{Binding EsUpdaterVisible,UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding EsUpdaterVisible,UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/> <!-- use hide instead of collapsed if you would like to open again this instance of window after close. -->
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>

关于c# - 我无法在 MVVM 中隐藏窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43988149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com