将控件的可见性绑定到IEnumerable的“Count” [英] Binding visibility of a control to 'Count' of an IEnumerable

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

问题描述

我有一个包含在IEnumerable中的对象列表。
我想根据此列表的计数设置控件的可见性。我试过:

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

但这不行。我尝试将MyList.Count绑定到文本块中的文本,以确保计数值正确,它是。它似乎没有正确设置可见性。

解决方案

你不能在绑定中使用逻辑或代码表达式(它期望一个 PropertyPath )。使用转换器或触发器,这是我会做的:

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

(您当然可以将风格重构为资源,如果你愿意的话。)


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}"

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的“Count”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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