禁用Grid - XAML中的所有按钮和输入

时间:2015-01-16 09:54:17

标签: c# xaml

我构建了一个Windows应用商店应用,我有以下xaml:

<Grid HorizontalAlignment="Center" VerticalAlignment="Top"    Margin="413,138,438,0" Height="550" Width="515" x:Name="FormContainer">

    <TextBox Margin="133,249,131,269" PlaceholderText="Email" BorderBrush="#FF755CB0" BorderThickness="1" Opacity="0.9" x:Name="Email"/>

    <PasswordBox HorizontalAlignment="Left" Margin="133,298,0,0" VerticalAlignment="Top" Width="251" Height="8" PlaceholderText="Password"  BorderBrush="#FF755CB0" BorderThickness="1" Opacity="0.9" x:Name="Password"/>
    <Button Content="Login" HorizontalAlignment="Left" Margin="130,358,0,0" VerticalAlignment="Top" Width="257" Height="50" Background="#FF235085" BorderBrush="#FF6749AC" BorderThickness="1" Foreground="White" Opacity="0.9" RequestedTheme="Light" Click="Login_Click"/>
    <TextBlock Margin="133,434,131,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="95" Width="251" FontSize="15" Foreground="Red" FontWeight="Light" x:Name="ErrorsPlaceholder" HorizontalAlignment="Center"/>
</Grid>

我希望当我单击按钮时,所有输入和按钮istelf都将被启用。有没有办法做到这一点,不是指定每个元素和diable分开?

感谢。

1 个答案:

答案 0 :(得分:0)

查看:

<UserControl x:Class="LoginView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="EnabledStyle" TargetType="{x:Type StackPanel}">
            <Setter Property="IsEnabled" Value="True" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding LoggingIn}" Value="true">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <StackPanel x:Name="FormContainer" Style="{StaticResource EnabledStyle}">
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="Email" Text="sample@email.net"/>
            <PasswordBox x:Name="Password" Password="password"/>
            <Button x:Name="LoginButton" Content="{Binding LoginButtonText, Mode=OneWay}" Click="OnLogin"/>
        </StackPanel>
        <TextBlock x:Name="Errors" Text="Sample Error Message"/>
    </StackPanel>
</UserControl>

代码背后:

    public partial class LoginView : UserControl, INotifyPropertyChanged
{
    public LoginView()
    {
        InitializeComponent();
        DataContext = this;
        LoggedIn = true;
        OnLogin(null, null);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public string LoginButtonText
    {
        get { return (string)GetValue(LoginButtonTextProperty); }
        set { SetValue(LoginButtonTextProperty, value); }
    }

    public static readonly DependencyProperty LoginButtonTextProperty =
        DependencyProperty.Register("LoginButtonText", typeof(string), typeof(LoginView));

    public bool LoggedIn
    {
        get { return (bool)GetValue(LoggedInProperty); }
        set { SetValue(LoggedInProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LoggedIn.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LoggedInProperty =
        DependencyProperty.Register("LoggedIn", typeof(bool), typeof(LoginView), new PropertyMetadata(OnLoggedIn));

    static void OnLoggedIn(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        var view = (LoginView)o;
        if (view == null) return;

        view.LoggedIn=(bool)e.NewValue;
        view.LoginButtonText = view.LoggedIn ? "Logoff" : view.LoggingIn ? "Logging In ..." : "Login";

        view.RaisePropertyChanged("LoginButtonText");
        view.RaisePropertyChanged("LoggedIn");
    }

    public bool LoggingIn
    {
        get { return (bool)GetValue(LoggingInProperty); }
        set { SetValue(LoggingInProperty, value); }
    }

    public static readonly DependencyProperty LoggingInProperty =
        DependencyProperty.Register("LoggingIn", typeof(bool), typeof(LoginView), new PropertyMetadata(OnLoggingIn));

    static void OnLoggingIn(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        var view = (LoginView)o;
        if (view == null) return;

        view.LoggingIn=(bool)e.NewValue;
        view.LoginButtonText = view.LoggedIn ? "Logoff" : view.LoggingIn ? "Logging In ..." : "Login";

        view.RaisePropertyChanged("LoginButtonText");
        view.RaisePropertyChanged("LoggingIn");
    }

    void OnLogin(object _, RoutedEventArgs __)
    {
        if (LoggedIn)
        {
            LoggedIn = false;
            LoggingIn = false;
        }
        else
        {
            LoggingIn = true;
            Task.Run(async () =>
            {
                await Task.Delay(3000); // simulating logon
                Dispatcher.Invoke(() =>
                {
                    LoggedIn = true;
                    LoggingIn = false;
                });
            });
        }
    }
}

我设置了三秒延迟来模拟登录,在此期间控件将全部被禁用,同时按钮显示&#34;登录...&#34;,之后按钮将显示& #34;注销&#34 ;.如果再次单击该按钮,它将返回说&#34;登录&#34;。