Have MultiBinding

  MultiBinding mb = new MultiBinding() { TargetNullValue = ""}; mb.Bindings.Add(new Binding("Model.DisplayString") { Source = this, TargetNullValue="" }); mb.Bindings.Add(new Binding("Model.ParentObject.DisplayString") { Source = this, TargetNullValue = "" }); mb.Bindings.Add(new Binding("Model.ParentObject.ParentObject.DisplayString") { Source = this, TargetNullValue = "" }); mb.Bindings.Add(new Binding("Model.ParentObject.ParentObject.ParentObject.DisplayString") { Source = this, TargetNullValue = "" }); mb.Bindings.Add(new Binding("Model.ParentObject.ParentObject.ParentObject.ParentObject.DisplayString") { Source = this, TargetNullValue = "" }); mb.Bindings.Add(new Binding("Model.ParentObject.ParentObject.ParentObject.ParentObject.ParentObject.DisplayString") { Source = this, TargetNullValue = "" }); ... mb.StringFormat = "{0} {1} {2} {3} {4} {5}"; this.SetBinding(TitleProperty, mb); 

however, for example, Model.ParentObject.ParentObject may be null , and the string in mb stops forming. What to do in this situation?

  • 3
    And if FallbackValue for bindings specify? - Alexey
  • @Alexey yes! it worked, thanks! - Dmitry Chistik

1 answer 1

Binding successfully returns a value if:

 Путь к источнику привязки разрешен. Преобразователь значений, если таковые имеются, способен преобразовать полученное значение. Полученное значение является допустимым для привязки свойства target (цель). 

(c) MSDN

Apparently in this case, one of these conditions is not met, which leads to a Fail, probably the Binding itself (and not its value) becomes Null and when you try to operate it, a NullReferenceException triggers somewhere in the depths of the binding methods and throws out null or empty string.

Installing BindingBase.FallbackValue solves this problem.