多个绑定以显示动态内容 [英] Multiple bindings to show dynamic content

查看:74
本文介绍了多个绑定以显示动态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建具有动态内容的布局时,我经常会做类似的事情:

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>

这里2个容器仅用于显示基于多种条件的内容,它是3个绑定与逻辑AND组合.

Here 2 containers are is used only to show something based on multiple conditions, it's 3 bindings combined with logical AND.

使用MVVM,可以创建单个属性并直接绑定到它:

Using MVVM it is possible to create single property and bind to it directly:

public bool ShowStatistic => IsLastSelectedItem && IsStatisticAvailable && HasStatistic;

但这并不总是可能/容易并且有缺点.我必须监视所有 conditional 属性的更改,并监视结果属性的上升通知.如果条件属性之一是静态的或特定于视图的,那么不可避免的麻烦是添加事件处理程序,订阅/取消订阅等,以使其在视图模型和/或上升通知中可用.

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.

昨天在SO的帮助下,我创建了漂亮的控件以添加动态内容.它具有单个bool依赖项属性来显示/隐藏其内容.现在,我正在思考如何避免像上面的示例那样为多个绑定嵌套多个此类控件.

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?

我可以考虑创建具有多个bool属性的自定义容器(MyGrid),该属性由多个绑定和一些其他属性用于指定表达式:ANDOR

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?

推荐答案

在这种情况下,多值转换器是理想的选择.

In this instance, a Multi-Value Converter is ideal.

类似以下内容:

public class MultiBoolToVisibilityConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if(values.All(v=>v is bool))
            return values.All(v=>(bool)v)?
                Visibility.Visible:
                Visibility.Hidden;
        else
            throw new ArgumentException("Cannot determine boolean state of non-boolean value");
    }
}

这样,您就拥有了一个可扩展的转换器,该转换器接受一个或多个布尔值,并且仅在值"数组中的所有项目均为true时才返回可见".

This way you've got an expandable converter that takes one or more boolean values and returns 'Visible' only when all items in the 'values' array are true.

在您的xaml中:

<TextBlock Text="{Binding Statistic}" >
    <TextBlock.Visibility>
        <MultiBinding Converter="{StaticResource MultiBoolToVisibilityConverter }">
            <Binding Path="IsLastSelectedItem" />
            <Binding Path="IsStatisticAvailable" />
            <Binding Path="HasStatistic" />
        </MultiBinding>
    </TextBlock.Visibility>
</TextBlock>

在您有多个标记来确定可见性的任何区域中都高度可重复使用,并且它也可以进行单元测试.

Highly re-usable in any area where you have multiple flags to determine visibility, plus it's unit-testable too.

这篇关于多个绑定以显示动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆