如何使用WPF中的绑定更改可见性

时间:2022-08-23 20:32:07

I am using MVVM Light WPF 4.

我正在使用MVVM Light WPF 4。

I have a ContentPresenter in my Home.xaml.

我的Home.xaml中有一个ContentPresenter。

<ContentPresenter Name="MDI" Content="{Binding WindowName, Mode=OneWay}">

I am binding user control to this in viewmodel like

我在viewmodel中绑定用户控件

public UserControl WindowName { get; set; }
    void ShowSalesEntry()
    {
        WindowName = null;
        WindowName = new SalesEntry();
        RaisePropertyChanged("WindowName");
    }

by using command in a menu click and it is binding fine.

通过在菜单单击中使用命令,它是绑定正常。

Now in the user control i have a button which i used to close (but to close i change the visibility to collapsed) by this way..

现在在用户控件中我有一个按钮,我曾经通过这种方式关闭(但关闭我将可见性更改为折叠)。

Visibility="{Binding visibility, Mode=OneWay}"

in the user control view model,

在用户控件视图模型中,

public SalesEntryViewModel()
    {
        visibility = Visibility.Visible;            
        cmdExitWindow = new RelayCommand(ExitWindow);
        RaisePropertyChanged("visibility");
    }

and the following to close (visibility to collapsed)

和以下关闭(崩溃的可见性)

public RelayCommand cmdExitWindow { get; set; }

    void ExitWindow()
    {
        visibility = Visibility.Hidden;
        RaisePropertyChanged("visibility");
    }

To exit (means visibility collapsed).. This is working fine upto this.

退出(意味着可见性崩溃)..这样做很好。

Problem is when i click the same page i mean to show the same user control, now this time the visibility is still collapsed. Even though i changed to visible in the load event.

问题是当我点击同一页面时我的意思是显示相同的用户控件,现在这次可见性仍然崩溃了。即使我在load事件中更改为可见。

How to solve this.. I am new to MVVM WPF.. Please help me..

怎么解决这个..我是MVVM WPF的新手..请帮帮我..

1 个解决方案

#1


1  

Problem is when i click the same page i mean to show the same user control, now this time the visibility is still collapsed. Even though i changed to visible in the load event.

问题是当我点击同一页面时我的意思是显示相同的用户控件,现在这次可见性仍然崩溃了。即使我在load事件中更改为可见。

Based on this comment and the code provided, you've either omitted code, or you've confused the purpose of the constructor.

根据此注释和提供的代码,您可能省略了代码,或者您混淆了构造函数的用途。

In your constructor, you have set the Visibility to Visible. You then have a method that sets the Visibility to Hidden, but there is nothing to ever set it back to Visible once this has occurred. The constructor only fires when the object is created. You need something to set the Visibility back at the appropriate time (ie. your comment "when i click the same page").

在构造函数中,您已将“可见性”设置为“可见”。然后,您有一个方法将Visibility设置为Hidden,但是一旦发生这种情况,就没有任何东西可以将它设置回Visible。构造函数仅在创建对象时触发。您需要在适当的时间设置可见性(即,当我点击同一页面时您的评论“)。

//Add these lines to the method/event that will show the control again
visibility = Visibility.Visible;
RaisePropertyChanged("visibility");

That's the best answer I can give based on what you've provided.

根据你提供的内容,这是我能给出的最佳答案。

#1


1  

Problem is when i click the same page i mean to show the same user control, now this time the visibility is still collapsed. Even though i changed to visible in the load event.

问题是当我点击同一页面时我的意思是显示相同的用户控件,现在这次可见性仍然崩溃了。即使我在load事件中更改为可见。

Based on this comment and the code provided, you've either omitted code, or you've confused the purpose of the constructor.

根据此注释和提供的代码,您可能省略了代码,或者您混淆了构造函数的用途。

In your constructor, you have set the Visibility to Visible. You then have a method that sets the Visibility to Hidden, but there is nothing to ever set it back to Visible once this has occurred. The constructor only fires when the object is created. You need something to set the Visibility back at the appropriate time (ie. your comment "when i click the same page").

在构造函数中,您已将“可见性”设置为“可见”。然后,您有一个方法将Visibility设置为Hidden,但是一旦发生这种情况,就没有任何东西可以将它设置回Visible。构造函数仅在创建对象时触发。您需要在适当的时间设置可见性(即,当我点击同一页面时您的评论“)。

//Add these lines to the method/event that will show the control again
visibility = Visibility.Visible;
RaisePropertyChanged("visibility");

That's the best answer I can give based on what you've provided.

根据你提供的内容,这是我能给出的最佳答案。