简体   繁体   中英

cascaded property binding using a converter in silverlight

I have the following problem,

I have a cascade of Items that can be selected by a user.

So for example I have an list of items A , an item from this list can be selected and is available via the property SelectedA .

Each A holds a list of items B . Each of those can selected, thus A provides a property SelectedB .

An item B then has a property which is boolean value C

Now I can bind directly to C by doing somthing like this.

Visibility="{Binding SelectedA.SelectedB.C, Converter={StaticResource BooleanToVisibilityConverter}}"

Now as you can see I use a converter to set the visibility. The problem is however, as long as there is no A and B selected, the converter will not be used. Therefor the Visibiltiy will still be set to visible.

To prevent that I would be forced to wrap the items in another GUI item so that I can then check for null values of the selected Items.

So I would have to make to wrap elements in this case for SelectedA to check if that is null and have a visibility to boolean converter and then another wrap element for SelectedB to see of that is also null. Only then could I use the actual element and the binding.

Is there anyway to force the converter to trigger with a null value, so the visibility will be set correctly ?

You can use TargetNullValue on the binding to provide a default value when the binded value is null . You can use FallbackValue to provide a value when the binding fails. Set either one or both to Visibility.Collapsed or Visibility.Hidden according to your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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