登录和标签控件可见性

时间:2015-03-30 13:31:59

标签: c# wpf tabs controls tabitem

我正在使用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()
    }
}
    }

1 个答案:

答案 0 :(得分:0)

如果您在代码隐藏中拥有逻辑,则可以轻松访问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;
            }
        }
    }