0

我正在使用 MVVM 模式研究 silverlight。在设计页面中,我有三个文本框和一个按钮。这里我的要求是如果 3 文本框为空或 null 表示按钮被禁用。如何实现这一目标..任何帮助..?

4

3 回答 3

0

在这里,我附上了我的编码.. 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

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

我的问题是:

1)它适用于第一个文本框。当我在第一个文本框中输入任何内容时,按钮处于禁用模式。但是,如果我移至第二个文本框,则表示该按钮已启用。2)我需要所有的文本框都将在仅启用按钮之后进行验证。如果任何一个文本框为空意味着按钮再次进入禁用模式..任何帮助..?

于 2012-10-26T12:26:40.600 回答
0

如果您使用的是 Silverlight 5,那么通过自定义标记扩展有一个非常好的 MultiBinding 解决方案:Silverlight 5 中的 MultiBinding
还有一个IMultiValueConverter允许您将绑定值的集合转换为单个值(在您的情况下为布尔值)。使用扩展程序和转换器,您可以轻松实现所需的功能。

于 2012-10-28T23:34:15.023 回答
0

在您的 VM 中创建一个公共属性,用于检查上述条件并相应地返回一个布尔值。将此属性绑定到您的按钮IsEnabled属性。

确保在PropertyChangedEvent每个条件状态更改后触发(即在文本框更改事件中)。

于 2012-10-26T12:12:27.690 回答