C# 如何显示一组过滤对象的一部分?
很抱歉,如果我复制,但我很难用这个词来表达一个像样的查询 我试图通过C# 如何显示一组过滤对象的一部分?,c#,wpf,xaml,filtering,C#,Wpf,Xaml,Filtering,很抱歉,如果我复制,但我很难用这个词来表达一个像样的查询 我试图通过ICollectionView过滤一组复杂对象,然后显示通过的每个对象,用它们的一个属性表示。在这个特定的示例中,集合是ComplexClass对象的列表,每个对象都包含Name和Number属性。我希望在列表框中用它们的名称表示它们,它们位于弹出窗口中 以下是主窗口的XAML代码: <Window x:Name="mainWindow" x:Class="TestWPF.MainWindow"
ICollectionView
过滤一组复杂对象,然后显示通过的每个对象,用它们的一个属性表示。在这个特定的示例中,集合是ComplexClass
对象的列表,每个对象都包含Name
和Number
属性。我希望在列表框
中用它们的名称
表示它们,它们位于弹出窗口
中
以下是主窗口的XAML代码
:
<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>
最后是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;
}
}
}
它当前所做的是显示每个对象,就像它们已经被ToString()-ed:“TestWPF.ComplexClass”一样
我实际上希望它们显示为:
教条
猫
球棒
苹果 按这个顺序
我能得到一些帮助吗?这里有几个问题 首先,您要绑定到XAML中的列表:
<ListBox ... ItemsSource="{Binding Source=strings}" ... />
您需要做一个或另一个:我建议使用XAML进行绑定,并从后面的代码中删除绑定内容
其次,ComplexClass
项正在进行字符串化,因为这是ListBoxItem
随内容提供时的默认行为(WPFListBox
中的项包装在ListBoxItem
元素中)。解决此问题的最简单方法是在列表框上设置DisplayMemberPath
属性:
<ListBox ... ItemsSource="{Binding Source=strings}" DisplayMemberPath="Name" ... />
好的,我两个都做了,现在,出于某种原因,列表框
什么也不显示。选择在那里,但我什么也看不到;每个选项都是空白的。更新:奇怪的是,当我把程序绑定代码放回原处时,每个选项都会出现。那么它现在能工作了吗?例如,显示名称而不是ToString版本?是的,但为什么我现在必须在两个位置进行绑定?你不应该。我个人会将绑定放在XAML中,并将其从代码中删除。在调用InitializeComponent
之前,我会将构造函数更改为创建stringList
和strings
,并将DataContext=this
在该方法调用之前,从列表框中删除DataContext
属性setter
XAML。DataContext=this
代码是否使该窗口的所有XAML事先知道这一点,以便我可以使用Source=“where”
,它会自动指向窗口
?我在WPF架构中仍然不安全。
Binding binding = new Binding();
binding.Source = strings;
testListBox.SetBinding(ListBox.ItemsSourceProperty, binding);
<ListBox ... ItemsSource="{Binding Source=strings}" DisplayMemberPath="Name" ... />