Wpf 如何筛选/查找与集合中的项目关联的字符串,并将匹配的字符串放入Silverlight中的texblock中?

Wpf 如何筛选/查找与集合中的项目关联的字符串,并将匹配的字符串放入Silverlight中的texblock中?,wpf,silverlight,silverlight-4.0,Wpf,Silverlight,Silverlight 4.0,我想知道是否有人可以分享信息或良好的样本过滤listboxitems基于什么是在文本框中键入。也许,它可以是一个不同的控件,更适合下面的场景 在我的场景中,我需要在texblock中键入一个短字符串。然后,单击“检查”按钮,该按钮将从集合中查找最接近的项目字符串值,并以文本块下方列表的形式显示这些匹配项。从显示的项目列表中选择任何项目都会将所选字符串/项目放入tetxblock。该行为与combox box非常相似 最后,我需要能够通过单击“添加”按钮将放置在texblock中的选定字符串/项目

我想知道是否有人可以分享信息或良好的样本过滤listboxitems基于什么是在文本框中键入。也许,它可以是一个不同的控件,更适合下面的场景

在我的场景中,我需要在texblock中键入一个短字符串。然后,单击“检查”按钮,该按钮将从集合中查找最接近的项目字符串值,并以文本块下方列表的形式显示这些匹配项。从显示的项目列表中选择任何项目都会将所选字符串/项目放入tetxblock。该行为与combox box非常相似

最后,我需要能够通过单击“添加”按钮将放置在texblock中的选定字符串/项目添加到另一个列表框中。任何想法都将受到高度赞赏。提前谢谢你

下面是我的XAML代码:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Class="FilterListItems.MainPage"
xmlns:local="clr-namespace:FilterListItems"
Width="640" Height="480">

<UserControl.Resources>   
    <local:Products x:Key="productCollection" />
    <CollectionViewSource x:Key="collProducts" Source="{Binding Source={StaticResource productCollection}, Path=DataCollection}">
</CollectionViewSource>
</UserControl.Resources> 
<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="10" Grid.Row="0">
  <TextBlock Text="Enter Partial Name: " />
  <TextBox Width="100" Name="txtName" />
  <Button Name="btnSearch" Content="Check" Click="btn_Check" />
  <Button Name="btnAdd" Content="Add" Click="btn_Add" Margin="9,0,0,0" />
</StackPanel>
<ListBox Margin="10" Grid.Row="1" Name="lstData" DisplayMemberPath="ProductName"  ItemsSource="{Binding Source={StaticResource collProducts}}" Visibility="Collapsed" />
<ListBox Margin="10" Grid.Row="2" Name="2stData" />

C#要生成集合,请执行以下操作:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        // Required to initialize variables
        InitializeComponent();
    }

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        //FilterData();
    }
}


public class Product
{
    public Product(int id, string name)
    {
      ProductId = id;
      ProductName = name;
    }

    public int ProductId { get; set; }
    public string ProductName { get; set; }
    }

public class Products : List<Product>
{


    public Products()
    {
        InitCollection();
    }

    public List<Product> DataCollection { get; set; }

    List<Product> InitCollection()
    {
        DataCollection = new List<Product>();

        DataCollection.Add(new Product(1, "aaa"));
        DataCollection.Add(new Product(2, "bbb"));
        DataCollection.Add(new Product(3, "ccc"));
        DataCollection.Add(new Product(4, "ddd"));
        DataCollection.Add(new Product(5, "eee"));
        DataCollection.Add(new Product(6, "fff"));
        DataCollection.Add(new Product(7, "hhh"));
        DataCollection.Add(new Product(8, "ggg"));

        return DataCollection;
    }
}
public部分类主页面:UserControl
{
公共主页()
{
//需要初始化变量
初始化组件();
}
私有无效BTN搜索单击(对象发送者,路由目标e)
{
//FilterData();
}
}
公共类产品
{
公共产品(int-id,字符串名称)
{
ProductId=id;
ProductName=名称;
}
public int ProductId{get;set;}
公共字符串ProductName{get;set;}
}
公共类产品:列表
{
公共产品()
{
InitCollection();
}
公共列表数据收集{get;set;}
列表InitCollection()
{
DataCollection=新列表();
数据收集。添加(新产品(1,“aaa”);
数据收集。添加(新产品(2,“bbb”);
数据收集。添加(新产品(3,“ccc”);
数据收集。添加(新产品(4,“ddd”);
数据收集。添加(新产品(5,“eee”);
数据收集。添加(新产品(6,“fff”);
数据收集。添加(新产品(7,“hhh”);
数据收集。添加(新产品(8,“ggg”);
返回数据收集;
}
}
从中查看

也检查


Bea Stollnitz是了解如何筛选集合的绝佳资源。你应该从她在“”上的帖子开始,以获得一个简单而清晰的图片。当你看完那篇文章后,只需使用她的博客上的搜索,就可以使用collectionviewsource搜索“筛选”收藏。

谢谢你对这篇文章的引用。这是一篇很棒的文章,很有用。看起来比丘的第二个建议可能更适合我的需要。我将两者都做实验。我只是想知道你是否可以建议通过点击按钮将选定的字符串从自动完成框移动到列表框。谢谢你,Biju。AutoCompleteBox应该能很好地满足我的需要。我只是想知道您是否可以建议如何通过单击按钮将所选字符串从自动完成框移动到列表框。再次感谢你。