与列表项绑定的可见性

时间:2015-03-26 14:31:23

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

List<MyItem> Reports = new List<MyItem>();

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

我使用ListBox

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

我想只显示CountAnswers大于0的项目。 我有100件商品,但只有少数商品有CountAnswers&gt; 0

1 个答案:

答案 0 :(得分:2)

然后使用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>