当 Silverlight MVVM 中的文本框为 Empty 或 Null 时,需要禁用我的按钮吗? [英] need my button disable when the textbox is Empty or Null in Silverlight MVVM?

查看:48
本文介绍了当 Silverlight MVVM 中的文本框为 Empty 或 Null 时,需要禁用我的按钮吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Silverlight5 中使用 MVVM 模式做了一个简单的应用程序.在设计页面中,我有三个文本框和一个按钮.这里我的要求是如果 3 文本框为空或 null 意味着按钮将被禁用.如何实现这一目标..有什么帮助..?我的问题是:

Hai i have doing one simple app in Silverlight5 with MVVM pattern. In design page i have three textbox and one button. here my requirement is if the 3 textbox is empty or null means the button is going to be disabled. how to achieve this.. any help..? My Problem is:

1) 它适用于第一个文本框.当我在第一个文本框中输入任何内容时,按钮处于禁用模式.但是如果我移动到第二个 TextBox 意味着该按钮已启用.2)我需要所有的文本框都将在仅启用按钮后进行验证.如果任何一个文本框为空意味着按钮再次进入禁用模式..任何帮助..?在这里我附上了我的编码..Xml:

1) It is worked for the 1st textbox. while i entered anything in the 1st textbox the button is in disabled mode. But if i moved to the 2nd TextBox means the button is enabled. 2) I need all the textbox will Validated after that only Button is enabled. If any one of the Textbox is Empty means the button is again went to disabled mode.. Any Help..? Here i have attached my coding.. Xaml:

<Button Content="Add"  Width="59" IsEnabled="{Binding ButtonIsEnabled}" Height="23" Margin="256,75,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" TabIndex="4" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <si:CallDataMethod Method="AddEmployee"/>
                    <si:SetProperty TargetName="LayoutRoot" PropertyName="Background" Value="LightBlue"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
        <TextBlock FontWeight="Bold" Height="26" HorizontalAlignment="Left" Margin="47,12,0,0" Name="textBlock1" Text="First Name:" VerticalAlignment="Top" Width="77" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="130,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Fname,Mode=TwoWay}" TabIndex="1" AcceptsReturn="False">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <si:CallDataMethod Method="ButtonIsEnabled"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox> 
        <TextBlock FontWeight="Bold" Height="25" HorizontalAlignment="Left" Margin="35,44,0,0" Name="textBlock2" Text="Second Name:" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="130,44,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Sname,Mode=TwoWay}" TabIndex="2" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <si:CallDataMethod Method="ButtonIsEnabled"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox> 
        <TextBlock FontWeight="Bold" Height="23" HorizontalAlignment="Left" Margin="45,75,0,0" Name="textBlock3" Text="Department:" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="130,75,0,0" Name="textBox3" VerticalAlignment="Top" Width="120"  Text="{Binding Dept,Mode=TwoWay}" TabIndex="3" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <si:CallDataMethod Method="ButtonIsEnabled"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox> 

视图模型代码:

EmployeeListViewModel.cs

EmployeeListViewModel.cs

public bool ButtonIsEnabled
        {
            get
            {
                return !(((string.IsNullOrEmpty(this.Fname)) && (string.IsNullOrEmpty(this.Sname)) && (string.IsNullOrEmpty(this.Dept))));
            }
        }
        private string _fname;
        public string Fname
        {
            get
            {
                return _fname;
            }
            set
            {
                if (_fname != value)
                {
                    _fname = value;
                    RaisePropertyChanged("Fname");
                    //RaisePropertyChanged("ButtonIsEnabled");
                }
                else
                {
                    _fname = value;
                    RaisePropertyChanged("Fname");
                }
            }
        }


private string _sname;
        public string Sname
        {
            get
            {
                return _sname;
            }
            set
            {
                if (_sname != value)
                {
                    _sname = value;
                    RaisePropertyChanged("Sname");
                    RaisePropertyChanged("ButtonIsEnabled");
                }
                else
                {
                    _sname = value;
                    RaisePropertyChanged("Sname");
                }
            }
        }
        private string _dept;
        public string Dept
        {
            get
            {
                return _dept;
            }
            set
            {
                if (_dept != value)
                {
                    _dept = value;
                    RaisePropertyChanged("Dept");
                    RaisePropertyChanged("ButtonIsEnabled");
                }
                else
                {
                    _dept = value;
                    RaisePropertyChanged("Dept");
                }
            }
        } 

推荐答案

如果您希望按钮仅在每个文本框有文本时启用,您需要像这样更改 ButtonIsEnabled 属性:

If you want the button to be enabled only when each textbox has text, you need to change your ButtonIsEnabled property like so:

public bool ButtonIsEnabled
{
    get
    {
        return !(((string.IsNullOrEmpty(this.Fname)) || (string.IsNullOrEmpty(this.Sname)) || (string.IsNullOrEmpty(this.Dept))));
    }
}

这篇关于当 Silverlight MVVM 中的文本框为 Empty 或 Null 时,需要禁用我的按钮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆