在Silverlight MVVM中文本框为空或空时需要禁用我的按钮吗?

时间:2012-10-26 12:34:22

标签: silverlight mvvm

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

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

ViewModel代码:

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

3 个答案:

答案 0 :(得分:3)

您需要删除触发器并在绑定中使用UpdateSourceTrigger,例如

Text="{Binding Fname,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

所以你的XAML看起来像:

<StackPanel>

    <Button Content="Add"  Width="59" IsEnabled="{Binding ButtonIsEnabled}" Height="23" Margin="256,75,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" TabIndex="4" />

    <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, UpdateSourceTrigger=PropertyChanged}" TabIndex="1" AcceptsReturn="False"/>

    <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, UpdateSourceTrigger=PropertyChanged}" TabIndex="2" />

    <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, UpdateSourceTrigger=PropertyChanged}" TabIndex="3" />

</StackPanel>

并且您的VM类需要看起来像:

    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
            {
            _fname = value;
            OnPropertyChanged("Fname");
            OnPropertyChanged("ButtonIsEnabled");  
            }
        }


    private string _sname;
    public string Sname
        {
        get { return _sname;}
        set
            {
            _sname = value;
            OnPropertyChanged("Sname");
            OnPropertyChanged("ButtonIsEnabled");          
            }
        }


    private string _dept;
    public string Dept
        {
        get { return _dept; }
        set
            {
            _dept = value;
            OnPropertyChanged("Dept");
            OnPropertyChanged("ButtonIsEnabled");
            }             
        }




    #region INotifyPropertyChanged Members

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

    #endregion /****** InotifyPropertyChanged Members *********************/

答案 1 :(得分:1)

如果您希望仅在每个文本框都有文本时启用该按钮,则需要更改ButtonIsEnabled属性,如下所示:

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

答案 2 :(得分:1)

在wpf我将执行以下操作,如果它在silverlight中工作则不知道

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

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

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