我想知道如果有人可以分享信息或良好的示例與篩選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;
}
}
謝謝Biju。 AutoCompleteBox應該適合我的需求。我只想知道是否可以通過單擊按鈕來建議如何將所選字符串從AutoCompleteBox移動到列表框。再次感謝你。 – vladc77 2011-03-03 08:10:53