When creating layout with dynamic content I often do something like:
<Grid Visibility="{Binding IsLastSelectedItem, Converter=...}" >
<Grid Visibility="{Binding IsStatisticAvailable, Converter=...}" >
<TextBlock Visibility="{Binding HasStatistic, Converter=...}"
Text="{Binding Statistic}" />
</Grid>
</Grid>
Here 2 containers are is used only to show something based on multiple conditions, it's 3 bindings combined with logical AND
.
Using MVVM it is possible to create single property and bind to it directly:
public bool ShowStatistic => IsLastSelectedItem && IsStatisticAvailable && HasStatistic;
But it's not always possible/easy and has downsides. I have to monitor for changes of all conditional properties and rise notification for resulting property. If one of conditional properties is static or view-specific, then it's unavoidable hassle of adding event handlers, subscribing/unsubscribing, etc. to make it available in viewmodel and/or rise notification.
Yesterday with SO help I've created nice control to add dynamic content. It has a single bool
dependency property to show/hide its content. Now I am thinking how to avoid nesting multiple of such controls for multiple bindings as in example above.
Question: what would be the best (reusable, easy to use, short, clear to understand) way to manage multiple binding used to create layout with dynamic content? I am probably lacking proper words to find similar questions.
I could think of multibinding and converter. Reusable? Hell no. Or not?
I could think of creating custom container (MyGrid
) with multiple bool
properties, used by multiple bindings and some other properties to specify expression: AND
, OR
, etc.
Maybe I am missing something obvious and easy?