开发者

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!


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜