hhravn
hhravn

Reputation: 1741

Best way to do a boolean-or-to-visibility

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!

Upvotes: 2

Views: 237

Answers (2)

Julian Dominguez
Julian Dominguez

Reputation: 2583

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>

Upvotes: 3

Lunivore
Lunivore

Reputation: 17602

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.

Upvotes: 5

Related Questions