简体   繁体   English

如何将IValueConverter添加到现有的Binding中

[英]How to add IValueConverter to an existing Binding

I have a TextBox and I try to add an IValueConverter to the TextProperty-Binding in code behind. 我有一个TextBox,我尝试在后面的代码中添加一个IValueConverter到TextProperty-Binding。

        var bindingExpression = TextBox.GetBindingExpression(TextBox.TextProperty);
        if (bindingExpression != null && bindingExpression.ParentBinding.Converter == null)
        {
            bindingExpression.ParentBinding.Converter = new MyConverter();
        }

In this step the following exception throws: Binding cannot be changed after it has been used. 在此步骤中,抛出以下异常: 绑定在使用后无法更改。 How can I access the binding earlier to change the property? 如何更早地访问绑定以更改属性?

  1. Create new Binding and apply it. 创建新的Binding并应用它。 You can get various properties from old Binding instance. 您可以从旧的Binding实例获取各种属性。

    or, 要么,

  2. Use Trigger to apply conditional Binding , one with and another without IValueConverter . 使用Trigger应用条件Binding ,一个用于另一个而不用IValueConverter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM