简体   繁体   English

从另一个用户控件(登录)更改WPF用户控件的可见性

[英]Change WPF user controls visibility from another user control (login)

I created an application with WPF and C# (no MVVM), I have multiple user controls containing grids, forms and others things. 我使用WPF和C#(无MVVM)创建了一个应用程序,我有多个用户控件,其中包含网格,表单和其他内容。 but finaly I wanted to add a login page, the way I started to build it, is to set by default all the user controls visibility in xaml to collapsed : 但是最后,我想添加一个登录页面(以开始构建它的方式)是默认将xaml中所有用户控件的可见性设置为折叠:

 Visibility="Collapsed"

and for the login user control to visible, and after login and in the C# code behind I want to change the visibility of other usercontrols to visible and set the login page to collapsed. 并将登录用户控件显示为可见,并且在登录后以及后面的C#代码中,我想将其他用户控件的可见性更改为“可见”,并将登录页面设置为折叠。

I've tried something like this, but it didn't work : 我已经尝试过类似的方法,但是没有用:

  Students studentsWin = new Students(); 
  studentsWin.Visibility = Visibility.Visible;

  Members MembersWin = new Members();
  MembersWin.Visibility = Visibility.Visible;

I have read in stackoverflow to use dependency proprety, and since I never used MVVM I don't know how to use it : I'm trying something like this : 我已经阅读了stackoverflow以使用依赖属性,并且由于我从未使用过MVVM,所以我不知道如何使用它:我正在尝试类似的方法:

    public static DependencyProperty WindowVisibilityProperty = DependencyProperty.Register("WindowVisibility", typeof(Visibility), typeof(MemberStats), null);

    public Visibility WindowVisibility
    {
        get
        {
            return (Visibility)GetValue(WindowVisibilityProperty);
        }
        set
        {
            SetValue(WindowVisibilityProperty, value);
        }
    }

But I don't know how to use WindowVisibility in the xaml of the user controls, any help would be appreciated . 但是我不知道如何在用户控件的xaml中使用WindowVisibility,将不胜感激。

  1. Simple approach. 简单的方法。 Use the following layout 使用以下布局

     <Grid> <Grid x:Name="UC_Container"> <!-- Rest of user controls go here --> </Grid> <uc:UserControl1 x:Name="LoginUC" Background="Aqua"/> </Grid> 

Hide the LogiUI itself in it's say SignIn button:click, like 将LogiUI本身隐藏在“登录”按钮中:

this.Visibility = Visibility.Collapsed

Your login-ui will be hidden and the grid lying below containing your rest of uc will be visible. 您的login-ui将被隐藏,位于下面的网格(包含其余的uc)将可见。

  1. The approach which you are thinking : You need a dependency property like ShowLoginUI in your Login UC.Then after user Signin successfully simply set it to false, as LoginUI is no longer needed. 您正在考虑的方法是:在登录UC中需要一个类似ShowLoginUI的依赖项属性,然后在用户登录成功后将其简单地设置为false,因为不再需要LoginUI。

Then in your MainWindow where you are using all the controls, bind login-ui container Grid's visibility to your LoginUI's ShowLoginUI dep-property. 然后在正在使用所有控件的MainWindow中,将login-ui容器Grid的可见性绑定到LoginUI的ShowLoginUI dep-property。

No need to set visibility of UC_Container as it will be hidden by LoginUI as we are using a Grid. 无需设置UC_Container的可见性,因为当我们使用网格时,它将被LoginUI隐藏。

LoginControl might look like : LoginControl可能类似于:

    public partial class LoginUC : UserControl
        {
            public bool ShowLoginUI
            {
                get { return (bool)GetValue(ShowLoginUIProperty); }
                set { SetValue(ShowLoginUIProperty, value); }
            }

            public static readonly DependencyProperty ShowLoginUIProperty =
                DependencyProperty.Register("ShowLoginUI", typeof(bool), typeof(UserControl1), new PropertyMetadata(true)); 

            ...

            private void SignIn_Click(object sender, RoutedEventArgs e)
            {
               // check login credentials
               // if success
               ShowLoginUI = false;
            }
       }

Have one panel to hold your user-controls and another panel for login UI. 有一个面板用于保存用户控件,而另一个面板用于登录UI。 Use Grid as top container as it allows it's children to stack on top of each other hiding those below. 使用网格作为顶部容器,因为它允许子级彼此堆叠,将下面的子级隐藏起来。 BooleanToVisibilityConverter is the in-built converter class which you can use. BooleanToVisibilityConverter是可以使用的内置转换器类。

<Window.Resources>
        <BooleanToVisibilityConverter x:Key="ConvBoolToVis" />
    </Window.Resources>
<Grid>
  <Grid x:Name="UC_Container">
       <!-- Rest of user controls go here -->
  </Grid>
  <Grid x:Name="LoginUI_Container" Visibility="{Binding ShowLoginUI, ElementName=LoginUC, Converter={StaticResource ConvBoolToVis}}">
        <uc:UserControl1 x:Name="LoginUC" Background="Aqua"/>
    </Grid>
</Grid>
  1. Best approach would be to use Frames. 最好的方法是使用框架。 Frames cleanly separate your Login-UI and rest of UCs. 框架将您的Login-UI和其余的UC完全分开。

Frames in WPF WPF中的帧

Students studentsWin = new MembresList(); 学生们的studyWin = new MembresList(); //Students is the user control name >studentsWin.Visibility = Visibility.Visible; //学生是用户控件名称> studentsWin.Visibility = Visibility.Visible;

Members MembersWin = new AjouterMembre();//Members is the user control name >MembersWin.Visibility = Visibility.Visible; 成员MembersWin = new AjouterMembre(); // Members是用户控件名称> MembersWin.Visibility = Visibility.Visible;

If "Students" is the user control name (meaning you added x:Name="Students" to the control in the XAML) then you will be able to access it in the code behind with the line : 如果“ Students”是用户控件名称(意味着您在XAML中向控件添加了x:Name =“ Students”),则可以在后面的代码中使用以下行访问它:

Students.visibility = Visibility.Visible;

I'm going to explore the rest of your code below, in hopes that it will help you. 我将在下面探索您的其余代码,希望对您有所帮助。 If you're pretty new to C# and WPF, I hope the following doesn't discourage you by making things seem overly complicated. 如果您是C#和WPF的新手,我希望以下内容不会因使事情看起来过于复杂而使您沮丧。

Does "Students studentsWin = new MembresList();" 是否“ Students studentsWin = new MembresList();” compile? 编译? MembresList would have to be a subclass of a Students class, which would be an odd class hierarchy. MembresList必须是Student类的子类,而该类是奇数类的层次结构。 But even if that line did compile, the line after that would definitely not compile. 但是,即使该行能够编译,此后的行也绝对不会编译。 "Students" would have to be the name of the class, but Visibility is an instance property of UI objects. “学生”必须是该类的名称,但“可见性”是UI对象的实例属性。 If Students was a subclass of UserControl, you could do 如果学生是UserControl的子类,则可以

Students myStudent = new Student(); //Assumes Students subclasses Usercontrol
myStudent.visibility = Visibility.Visible; //This compiles but is useless

The code above would compile, but would be useless, because the new myStudent object hasn't been added anywhere to your form. 上面的代码可以编译,但是将无用,因为尚未将新的myStudent对象添加到窗体的任何位置。 It's just an object that lives within the scope of the method that defines it. 它只是一个对象,位于定义它的方法的范围之内。

Dependency Properties are a useful thing, but I wouldn't worry about them until you have a good handle on C# and WPF in general. 依赖属性是一件有用的事情,但是除非您对C#和WPF有了一个很好的了解,否则我不会担心它们。 That stuff is pretty advanced. 那东西是相当先进的。

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

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