2011-03-03 21 views
0

我想知道如果有人可以分享信息或良好的示例與篩選listboxitems基於什麼在文本框中鍵入。也許,它可能是一個不同的控制,更適合下面的情況。如何過濾/查找與集合中項目相關的字符串,並將匹配的字符串放入Silverlight中的texblock中?

在我的方案中,我需要在texblock中鍵入一個短字符串。然後,點擊'檢查'按鈕,該按鈕將從集合中找到最接近的字符串值,並以文本塊下方列表的形式顯示這些匹配。從顯示的項目列表中選擇任何項目將把選定的字符串/項目放入tetxblock中。該行爲與combox盒非常相似。

最後,我需要能夠通過單擊'添加'按鈕將添加到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; 
    } 
} 

回答

1

Bea Stollnitz是瞭解如何過濾集合的好資源。您應該從「How do I filter items from a collection?」的帖子開始,以獲得簡單清晰的圖片。當您完成這篇文章時,只需在她的博客上使用搜索功能,使用collectionviewsource搜索「過濾」集合即可。

+0

感謝您對本文的參考。這是偉大的文章和有用的。看起來Biju提出的第二個建議可能更適合我的需求。我會試驗兩個。我只想知道是否可以通過單擊按鈕將選定的字符串從AutoCompleteBox移動到列表框。 – vladc77 2011-03-03 08:09:36

相關問題