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

本文关键字:控件 用户 更改 WPF 可见性 另一个 登录 | 更新日期: 2023-09-27 18:16:03

我用WPF和c#(没有MVVM)创建了一个应用程序,我有多个用户控件包含网格、形式和其他东西的。但最后我想添加一个登录页面,我开始构建它的方式是将xaml中所有用户控件的可见性默认设置为collapse:

 Visibility="Collapsed"

和为登录用户控件可见,在登录后,在c#代码后面,我想改变其他用户控件的可见性为可见,并设置登录页面为折叠

我试过这样做,但它没有工作:

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

我已经在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);
        }
    }

但是我不知道如何在用户控件的xaml中使用WindowVisibility,

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

studentsWin = new MembresList();//Students是用户控件名>studentsWin。

Members MembersWin = new ajoutermember ();//Members是用户控件名>MembersWin。

如果"Students"是用户控件名(意味着你在XAML中为控件添加了x: name ="Students"),那么你将能够在后面的代码中访问它,行:

Students.visibility = Visibility.Visible;
我将在下面探索您的代码的其余部分,希望它会对您有所帮助。如果你是c#和WPF的新手,我希望下面的内容不会因为让事情看起来过于复杂而使你气馁。

"Students studentsWin = new memberreslist();"编译吗?memberreslist必须是Students类的子类,这将是一个奇怪的类层次结构。但是,即使这一行确实编译了,它之后的那一行也肯定不会编译。"Students"必须是类名,但Visibility是UI对象的实例属性。如果Students是UserControl的子类,你可以做

Students myStudent = new Student(); //Assumes Students subclasses Usercontrol
myStudent.visibility = Visibility.Visible; //This compiles but is useless
上面的代码可以编译,但是没有用,因为新的myStudent对象还没有添加到表单的任何地方。它只是一个存在于定义它的方法范围内的对象。

依赖属性是一个有用的东西,但我不会担心他们,直到你有一个很好的处理c#和WPF的一般。

  1. 简单的方法。使用下面的布局

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

隐藏logui本身在它的说SignIn按钮:点击,像

this.Visibility = Visibility.Collapsed

你的登录界面将被隐藏,下面的网格包含你的uc的其余部分将是可见的。

  • 你正在考虑的方法:你需要一个依赖属性,如ShowLoginUI在你的登录UC。然后,在用户登录成功后,只需将其设置为false,因为不再需要LoginUI。
  • 然后在你使用所有控件的主窗口中,将login-ui容器网格的可见性绑定到你的LoginUI的ShowLoginUI深度属性。

    不需要设置UC_Container的可见性,因为它将被LoginUI隐藏,因为我们使用的是Grid。

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

    有一个面板容纳您的用户控件和另一个面板登录UI。使用网格作为顶部容器,因为它允许它的孩子堆叠在彼此的顶部隐藏那些下面。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>
    
    最好的方法是使用帧。框架清晰地分离了登录ui和其他uc。

    WPF中的帧