使用MVVM禁用文本框的文本框值?

时间:2021-05-08 20:37:14

we are using MVVM Pattern in our application, we had a screen with 10 text Boxes , whenever they entered value in any one text box others should be disabled. Whenever i enter the value the event gets fired and i am able to disable other 9 text boxes.

我们在我们的应用程序中使用MVVM模式,我们有一个带有10个文本框的屏幕,每当他们在任何一个文本框中输入值时,其他人都应该被禁用。每当我输入值时,事件就会被触发,我可以禁用其他9个文本框。

Here Comes the Problem , The event Doesn't get fired when i remove/Backspace the value

这就是问题所在,当我删除/退格值时,事件不会被触发

Example :
Say i have 3 Text Boxes TB1,TB2,TB3 for each of these Text Boxes the Text Property Binding is Like Text="{Binding TextBox1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" For TB1,... similary to other Text BoxesTB2 and TB3.So Now when i write some thing in the UI in TB1 the Set event is fired for TextBox1 Property and i am disabling the TB2 and TB3 .Now, When i Delete/Backspace a Single Number/Char in TB1 the Set Event is Not Fired for Enabling the other 2 Text Boxes since there is No Value in TextBox TB1.

示例:假设我有3个文本框TB1,TB2,TB3为每个文本框文本属性绑定类似于文本=“{绑定TextBox1,模式= TwoWay,UpdateSourceTrigger = PropertyChanged}”对于TB1,...类似于其他Text BoxesTB2和TB3.So现在当我在TB1的UI中写一些东西时,为TextBox1属性触发了Set事件,我正在禁用TB2和TB3。现在,当我删除/退格TB1中的单个数字/字符集时由于TextBox TB1中没有值,因此未启用事件以启用其他2个文本框。

View : 

<StackPanel orintation = "Horizontal" Margin = "20,0,20,0">
<TextBox Text = {Binding TextBox1,Mode = TwoWay, UpdateSourceTrigger = PropertyChanged} IsEnbaled={Binding IsTextBox1Enabled}>
<TextBox Text = {Binding TextBox2,Mode = TwoWay, UpdateSourceTrigger = PropertyChanged} IsEnbaled={Binding IsTextBox2Enabled}>
<TextBox Text = {Binding TextBox3,Mode = TwoWay, UpdateSourceTrigger = PropertyChanged} IsEnbaled={Binding IsTextBox3Enabled}>
</StackPanel>

ViewModel:

private bool _isTextBox1Enabled = true;
private bool _isTextBox2Enabled = true;
private bool _isTextBox3Enabled = true;

/// Encapsulating Above 3 _isTextBoxEnabled Properties  

private string _textBox1;
private String _textBox2;
private string _textbox3;

Public String TextBox1
{
    get { Return _textBox1;}
    set 
    {
        _textBox1 = value;
        if (TextBox1 > 0)
        {
            _isTextBox2Enabled  = false;
            _isTextBox3Enabled  = false;
        }   
        else
        {
            _isTextBox2Enabled  = true;
            _isTextBox3Enabled  = true;
        }           
        NotifyPropertyChanged("TextBox1");
    }
}

Public String TextBox2
{
    get { Return _textBox2;}
    set 
    {
        _textBox2 = value;
        if (TextBox2 > 0)
        {
            _isTextBox1Enabled  = false;
            _isTextBox3Enabled  = false;
        }   
        else
        {
            _isTextBox1Enabled  = true;
            _isTextBox3Enabled  = true;
        }           
        NotifyPropertyChanged("TextBox2");
    }
}

Public String TextBox3
{
    get { Return _textBox3;}
    set 
    {
        _textBox3 = value;
        if (TextBox3 > 0)
        {
            _isTextBox2Enabled  = false;
            _isTextBox1Enabled  = false;
        }   
        else
        {
            _isTextBox2Enabled  = true;
            _isTextBox1Enabled  = true;
        }       
        NotifyPropertyChanged("TextBox3");
    }
}

1 个解决方案

#1


1  

To enable updates on any change of the text value (without having to lose focus) add the UpdateSourceTrigger flag to the binding.

要对文本值的任何更改启用更新(不必失去焦点),请将UpdateSourceTrigger标志添加到绑定。

<TextBox Text="{Binding MyTextProperty, UpdateSourceTrigger=PropertyChanged}" />

#1


1  

To enable updates on any change of the text value (without having to lose focus) add the UpdateSourceTrigger flag to the binding.

要对文本值的任何更改启用更新(不必失去焦点),请将UpdateSourceTrigger标志添加到绑定。

<TextBox Text="{Binding MyTextProperty, UpdateSourceTrigger=PropertyChanged}" />