如何显示一组过滤对象的一部分?

时间:2015-01-15 10:33:46

标签: c# wpf xaml filtering

很抱歉,如果我复制,但我很难用一个好的方法来判断这个问题。

我尝试通过ICollectionView过滤一组复杂对象,然后显示完成它的每个对象,由其中一个属性表示。在此特定示例中,集合是ComplexClass个对象的列表,每个对象都包含NameNumber属性。我希望ListBox代表每个人PopupName位于MainWindow内。

这是<Window x:Name="mainWindow" x:Class="TestWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:test="clr-namespace:TestWPF" Title="MainWindow" Height="350" Width="525"> <Window.Resources> </Window.Resources> <Grid x:Name="mainGrid"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackPanel x:Name="buttonStack"> <Button x:Name="popupButton" Click="popupButton_Click" Content="Pop-up!" VerticalAlignment="Top"/> <Button x:Name="randomInsertButton" Click="randomInsertButton_Click" Content="Random Addition!"/> </StackPanel> <Popup x:Name="testPopup" PlacementTarget="{Binding ElementName=popupButton}" PopupAnimation="Scroll" Placement="Left" AllowsTransparency="True" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid> <Button x:Name="popupsButton" Content="Button" Width="75" HorizontalAlignment="Left" VerticalAlignment="Top"/> <ListBox x:Name="testListBox" Height="100" DataContext="{Binding ElementName=mainWindow}" ItemsSource="{Binding Source=strings}" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionChanged="testListBox_Selected"/> </Grid> </Popup> </Grid> </Window> 的XAML代码:

MainWindow

namespace TestWPF { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { static List<ComplexClass> stringList = new List<ComplexClass>(); public ObservableCollection<ComplexClass> strings; public MainWindow() { InitializeComponent(); stringList.Add(new ComplexClass("apple",0)); stringList.Add(new ComplexClass("bat",2)); stringList.Add(new ComplexClass("cattle",6)); stringList.Add(new ComplexClass("dogma",5)); strings = new ObservableCollection<ComplexClass>(stringList); Binding binding = new Binding(); binding.Source = strings; testListBox.SetBinding(ListBox.ItemsSourceProperty, binding); } private void popupButton_Click(object sender, RoutedEventArgs e) { ICollectionView view = CollectionViewSource.GetDefaultView(strings); view.Filter = //null; (o) => { return (o as ComplexClass).Name!=string.Empty; }; view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Descending)); testPopup.IsOpen = !testPopup.IsOpen; } private void randomInsertButton_Click(object sender, RoutedEventArgs e) { Random r = new Random(); stringList.Add(stringList[r.Next(0, stringList.Count)]); strings.Add(stringList.Last()); } private void testListBox_Selected(object sender, RoutedEventArgs e) { ComplexClass selected =(ComplexClass)(sender as ListBox).SelectedItem; stringList.Add(selected); strings.Add(selected); } } } 代码隐藏:

ComplexClass

最后,namespace TestWPF { public class ComplexClass { public string Name { get; private set; } public int Number { get; private set; } public ComplexClass(string name, int number) { Name = name; Number = number; } } } 代码:

{{1}}

它目前正在做的是将每个对象显示为ToString() - ed:&#34; TestWPF.ComplexClass&#34;。

我实际上希望它们显示为:

教条

蝙蝠
苹果

按顺序

我可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这里有几个问题。

首先,您绑定到XAML中的列表:

<ListBox ... ItemsSource="{Binding Source=strings}" ... />

然后还在代码中设置绑定:

Binding binding = new Binding();
binding.Source = strings;
testListBox.SetBinding(ListBox.ItemsSourceProperty, binding);

你需要做一个或另一个:我建议在XAML中绑定,并从代码隐藏中删除绑定内容。

其次,ComplexClass项是ToString-ified,因为当提供内容时,这是ListBoxItem的默认行为(WPF ListBox中的项目包含在{{1}内元素)。解决此问题的最简单方法是在ListBoxItem上设置DisplayMemberPath属性:

ListBox