WPF MultiBinding - UnsetValue Issue

2019-04-06 00:44发布

问题:

I have a TextBlock. When its Text is bound as:

<Binding Path="Applicant2.Surname"/>

It works fine, however I want to include the Forenames so changed the binding to:

<MultiBinding StringFormat="{}{0} {1}">
    <Binding Path="Applicant2.Forenames"/>
    <Binding Path="Applicant2.Surname"/>
</MultiBinding>

This displays {DependencyProperty.UnsetValue} {DependencyProperty.UnsetValue} until the value is set the first time.

How can I stop this? Why do I not get the problem with the first simple binding?

回答1:

for a multibinding you need to add a fallback value if it is just blank then you can simply do:

<MultiBinding StringFormat="{}{0} {1}">
    <Binding Path="Applicant2.Forenames" FallbackValue=""/>
    <Binding Path="Applicant2.Surname" FallbackValue=""/>
</MultiBinding>