views:

40

answers:

2

I have a control that i want to be visible only if atleast one of a series of properties return true. I was about to implement my own BooleanOrToVisibilityMultiConverter, but it feels like there must be a better (and completely obvious) way to do this.

Please enlighten me!

+5  A: 

The MVVM way of doing this is to return a single boolean from your model which contains the logic which works out whether your control should be visible or not.

Normally if I have this kind of logic, it's because there's some domain concept I'm trying to express - for instance:

  • it's in this country
  • it's ready to process
  • it still needs some work
  • it is a complete outfit
  • all authors are attributed

etc.

By keeping the logic which leads to the domain concept out of the Gui, you make it easier to test and to maintain. Otherwise you'll end up replicating that same logic everywhere you use the domain concept, and it's not so easy in Xaml.

Lunivore
Or if it's really a view thing, then return a single boolean from the ViewModel. Either way you can then just use a BooleanToVisibilityConverter.
Jackson Pope
+1  A: 

Well, using a converter is one option, and you can also use Multi Data Triggers (no one solution is better, depends on your scenario).

You need to set this in your control's (or DataTemplate's) Triggers collection:

    <DataTemplate.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsInstalled}" Value="True"/>
                <Condition Binding="{Binding IsOwned}" Value="False" />
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter TargetName="SkullImage" Property="Visibility" Value="Visible" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </DataTemplate.Triggers>
Julian Dominguez
+1. I did not, actually, know about the MultiDataTrigger. This will come in handy in other places too.
hhravn