简体   繁体   English

我无法在MVVM中隐藏窗口

[英]I can't hide the window in MVVM

I have the startup window in a WPF application, and I have this code in my view: 我在WPF应用程序中有启动窗口,我在我的视图中有这个代码:

     <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>

My ViewModel: 我的ViewModel:

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");
                }
            }
        }

When I click the aceptar button, the title of the window is changed, but the windows is still visible. 单击“aceptar”按钮时,窗口的标题会更改,但窗口仍然可见。

I would like to hide the window in some cases from the view model. 我想在某些情况下从视图模型中隐藏窗口。 How I could do that? 我怎么能这样做?

Thanks. 谢谢。

If you wouldn't like to use converter, just xaml part: 如果您不想使用转换器,只需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>

Visibility is not a boolean type. 可见性不是布尔类型。 You can use a converter to accomplish that. 您可以使用转换器来实现这一目标。 Converter: 转换器:

[ValueConversion(typeof(bool), typeof(Visibility))]
public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Than your XAML will look something like this: 比你的XAML看起来像这样:

<Window x:Class="MyView"
                Name="ucPrincipal"
                Title="{Binding Titulo}"
                Visibility="{Binding EsUpdaterVisible, Converter={StaticResource visibilityConverter}}">

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

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