views:

1168

answers:

2

Hello, I was wondering if there was a way to have another binding as a fall back value ?

I'm trying to do something like this:

<Label Content="{Binding SelectedItem.Name, ElementName=groupTreeView, FallbackValue={Binding RootGroup.Name}}" />

If any ones got another trick to pull it off that would be great.

Thanks, Raul

A: 

Under what conditions would you like it to use the Fallback value? How would you determine that a binding has failed? A binding is still valid even if it's bound to a null value.

I think a good bet may be to use a converter to convert to a default value if the binding returns null. I'm not sure how you could default to another bound value though.

Check out converters here

James Hay
+6  A: 

What you are looking for is something called PriorityBinding (#6 on this list)

(from the article)

The point to PriorityBinding is to name multiple data bindings in order of most desirable to least desirable. This way if the first binding fails, is empty and/or default, another binding can take it's place.

e.g.

<TextBox>
    <TextBox.Text>
        <PriorityBinding>
            <Binding Path="LastNameNonExistant" IsAsync="True" />
            <Binding Path="FirstName" IsAsync="True" />
        </PriorityBinding>
    </TextBox.Text>
</TextBox>
IanR
Awesome! That's exactly what I was looking for.Thanks!
HaxElit
The problem is that PriorityBinding treats a null string as a successful binding
Shimmy