条件绑定

时间:2018-05-30 08:24:19

标签: c# wpf xaml

我有一些TextBox es,它们都绑定到ViewModel中的字符串属性TheText

<TextBox x:Name="t1" Text="{Binding TheText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="t2" Text="{Binding TheText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="t3" Text="{Binding TheText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

ViewModel:

string theString = String.Empty;

public string TheText
{
    get { return this.theString; }
    set
    {
        if (this.theString != value)
        {
            this.RaisePropertyChanged();
            this.theString = value;
        }
    }
}

我正在尝试执行以下操作:

if(something)
   then bind only t1 and t2 and cancel binding of t3

有可能吗?

1 个答案:

答案 0 :(得分:2)

为什么不创建3个属性并在需要时绑定它们?

你将使用TheText1,TheText2和TheText3进行3次初始化,但这并不错。您只需根据需要设置“TheTextX”。

如果您确实需要“删除”绑定,请执行以下操作:

BindingOperations.ClearBinding(yourTextBoxToDeleteBind, TextBox.TextProperty)
相关问题