我想知道是否有人可以根据文本框中输入的内容过滤listboxitems来分享信息或样本。也许,它可以是一个不同的控件,更适合下面的场景。
在我的场景中,我需要在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;
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
Bea Stollnitz是了解如何过滤馆藏的绝佳资源。您应该从“How do I filter items from a collection?”上的帖子开始,以获得简单明了的图片。完成该文章后,只需使用她博客上的搜索,使用collectionviewsource搜索“过滤”集合。