【问题标题】:Visibility binding with list item与列表项的可见性绑定
【发布时间】:2015-05-30 15:02:36
【问题描述】:
List<MyItem> Reports = new List<MyItem>();

public class MyItem
    {
        public int CountAnswers{ get; set; }
        public DateTime DateTimeStartTime { get; set; }
    }

我正在使用BindingListBox 中显示它:

<ListBox Name="QuestionList" ItemsSource="{Binding Reports}">
    <ListBox.ItemTemplate>
        <DataTemplate >            
             <TextBlock Text="{Binding CountAnswers}"/>                      
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我只想显示 CountAnswers 大于 0 的项目。 我有 100 个项目,但只有少数项目有 CountAnswers > 0。

【问题讨论】:

    标签: c# windows-phone-8 binding listbox visibility


    【解决方案1】:

    然后使用 LINQ ...

    public IEnumerable<MyItem> ReportsWithAnwers
    {
        get
        {
           return Reports.Where(x => x.CountAnswers > 0);
        }
    }
    
    <ListBox Name="QuestionList" ItemsSource="{Binding ReportsWithAnwers}">
        <ListBox.ItemTemplate>
            <DataTemplate >            
                 <TextBlock Text="{Binding CountAnswers}"/>                      
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

    • 如果我还想显示 DateTimeStartTime 怎么办
    • 将您的文本块包装到堆栈面板中并添加下一个并将其绑定为与 countsanswers 属性相同
    猜你喜欢
    • 2020-02-06
    • 2018-07-15
    • 2011-12-04
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 2018-07-28
    • 2014-08-20
    • 1970-01-01
    相关资源
    最近更新 更多