登录和选项卡控制可见性

Login and tab control visibility

我正在用 C# 编写程序。我正在尝试制作一个简单的登录程序。例如,当用户的详细信息正确时,则显示选项卡项的内容,如果不正确,则不显示选项卡项的内容。代码在

    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="60"/>
        <RowDefinition Height="60"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" Orientation="Horizontal">
        <Label Name="lbluser" Content="User" Height="30" Width="50" />
        <TextBox Name="txtuser" Width="180" Height="30"/>

    </StackPanel>
    <StackPanel Grid.Row="2" Orientation="Horizontal">
        <Label Name="lblpass" Content="Password" Height="30"/>
        <PasswordBox Name="psw" Height="30" Width="180"/>
    </StackPanel>
    <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center">
        <Button Name="btnclick" Content="Click" Width="80" Height="30" Click="btnclick_Click" />
        <Button Name="btncancel" Content="Cancel" Width="80" Height="30" Margin="10,0,0,0" Click="btncancel_Click" />
        <Button Name="btnclose" Content="Close" Width="80" Height="30" Margin="10,0,0,0" Click="btnclose_Click" />
    </StackPanel>
    <StackPanel Grid.Row="4" >
        <TextBox Name="txtres" Height="30" Width="200"/>
    </StackPanel>
    <StackPanel Grid.Row="5">
        <TabControl Margin="0,10,0,0">
            <TabItem Header="Tab I" >
                <StackPanel>
                    <TextBox Name="txt1" Width="250" Height="30"/>
                    <Button Name="btn1" Width="80" Height="30" Content="Display"/>
                </StackPanel>
            </TabItem>
        </TabControl>
    </StackPanel>

后面的代码

     {
        InitializeComponent();
    }

    private void btnclick_Click(object sender, RoutedEventArgs e)
    {
        {
            if (txtuser.Text == "TEST" && psw.Password == "TEST")
            {
                txtres.Text = "   You are logged in";
                txtres.Foreground = Brushes.Green;
                txtres.FontSize = 14;
                MessageBox.Show("You are logged in");
            }

            else
            {
                txtres.Text = "    You are not logged in";
                txtres.Foreground = Brushes.Red;
                txtres.FontSize = 14;
                MessageBox.Show("You are not logged in");
            }
        }
    }

    private void btncancel_Click(object sender, RoutedEventArgs e)
    {
        txtuser.Text = "";
        psw.Password = "";
    }

    private void btnclose_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Shutdown()
    }
}
    }

如果您在代码隐藏中有逻辑,您可以轻松访问 UI 元素的实例。

您可以为标签项设置名称:

<TabItem x:Name="LoginSuccessTab" Header="Tab I"  >

然后登录失败时隐藏

private void btnclick_Click(object sender, RoutedEventArgs e)
    {
        {
            if (txtuser.Text == "TEST" && psw.Password == "TEST")
            {
                txtres.Text = "   You are logged in";
                txtres.Foreground = Brushes.Green;
                txtres.FontSize = 14;
                MessageBox.Show("You are logged in");
                LoginSuccessTab.Visibility = Visibility.Visible;
            }

            else
            {
                txtres.Text = "    You are not logged in";
                txtres.Foreground = Brushes.Red;
                txtres.FontSize = 14;
                MessageBox.Show("You are not logged in");
                LoginSuccessTab.Visibility = Visibility.Collapsed;
            }
        }
    }