简体   繁体   中英

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:

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

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.

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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