本文介绍了绑定控件的可视性IEnumerable的“计数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须包含在一个IEnumerable<对象的列表;>。
我想设置基于此列表的计数控制的可见性。我曾尝试:

I have a list of objects contained in an IEnumerable<>.I would like to set the visibility of a control based on the count of this list. I have tried:

 Visibility="{Binding MyList.Count>0?Collapsed:Visible, Mode=OneWay}"

但是,这并不正常工作。我试着结合MyList.Count在文本块中的文本,以确保计数值是正确的,它是。它只是似乎没有正确设置的知名度。

But this doesn't work. I tried binding MyList.Count to the text in a text block to ensure that the count value was correct, and it is. It just doesn't seem to set the visibility correctly.

推荐答案

您不能使用绑定逻辑或代码表达式(它需要一个的)。无论是使用或触发器,这就是我这样做:

You cannot use logical or code-expressions in bindings (it expects a PropertyPath). Either use a converter or triggers, which is what i would do:

<YourControl.Style>                     
    <Style TargetType="YourControl">
        <Setter Property="Visibility" Value="Collapsed" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding MyList.Count}" Value="0">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</YourControl.Style>

(当然你可以重构的风格融入了的如果你想。)

(You can of course refactor the style into a resource if you wish.)

这篇关于绑定控件的可视性IEnumerable的“计数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:18