Binding visibility of a control to 'Count' of an IEnumerable

前端 未结 3 1726
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-19 08:55

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:

 Visibi         


        
3条回答
  •  温柔的废话
    2021-02-19 09:43

    There is three ways:

    1. to use Triggers mentioned by H.B.
    2. to use convertors by implementing IValueConverter in a class and setting the Converter property of Binding to an instance of IValueConverter in that class
    3. to define a property in your ViewModel to directly return the Visibility state.

    You could always use Triggers method and it always is a good approach. The third method is useful(and is best) when you are using MVVM pattern (and you are not restricting yourself from referencing UI related assemblies in your ViewModel) I suggest using Triggers, but if you dont want to make your xaml dirty by that much markup codes use converters.

提交回复
热议问题