筛选列表框的项源

本文关键字:列表 筛选 | 更新日期: 2023-09-27 18:18:46

我正在做这个例子:http://code.msdn.microsoft.com/wpapps/Beginners-for-how-to-use-45690caa

我在表中插入了一个新列,名为"category"

现在我只需要在列表框中查看category == "card"

这是列表框:

<ListBox x:Name="ContactListBox" Grid.Row="2">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="100" />
                                        <ColumnDefinition Width="200" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Image Source="Images'open.png" Margin="-30, -18, 0, 0" Tap="Edit_Tap" Height="75" Width="75"/>
                                    <TextBlock Text="{Binding Name}" Tap="Edit_Tap" Grid.Column="1" FontSize="{StaticResource PhoneFontSizeLarge}" TextAlignment="Left"/>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

这是列表框的来源:

void Customers_Loaded(object sender, RoutedEventArgs e)
    {
        using (AddressBookDataContext dc = new AddressBookDataContext())
        {
            ContactListBox.ItemsSource = from c in dc.Contacts select c;
        }
    }

i've try to change it in:ContactListBox。ItemsSource = from c in dc。c.category == "card" select c;但我不工作,我能做什么?

谢谢大家,很抱歉我的英语不好

筛选列表框的项源

您没有提到您得到的异常,但听起来您可能会得到' NotSupportedException:成员"[whatever]"不支持转换为SQL错误"。

如果你搜索这个异常,你会发现很多答案都是一个共同的主题——本质上,Linq2SQL不知道如何将"Type"转换成SQL,所以它失败了。在您的情况下,您可以通过使用Linq2SQL和Linq2Object:

来解决这个问题。
// L2S from the DB, transferred to an Enumerable
var itemSource = (from c in dc.Contacts select c).AsEnumerable();
ContactListBox.ItemsSource = itemSource.Where(c => c.Type == "q");
// or full form Linq
ContactListBox.ItemsSource = from c in itemSource where c.Type == "q" select c;

在我的例子中,我没有添加一列,我只是添加了多个不同Type的联系人,并只过滤了Type"q"的联系人。