C# 将ObservableCollection筛选到多个列表框
在我的项目中,我目前有一个C# 将ObservableCollection筛选到多个列表框,c#,wpf,mvvm,observablecollection,collectionviewsource,C#,Wpf,Mvvm,Observablecollection,Collectionviewsource,在我的项目中,我目前有一个observedcollection,它填充在我的ViewModel构造函数中。此ObservableCollection包含一个具有两个属性(两个字符串)的自定义对象 当前,XAML/View对应项拥有两个单独的列表框,它们都绑定到一个数据模板,该模板选择要在列表框中显示为条目的属性。在本例中,它显示“propertyOne” 是否可以有一个数据模板,它可以根据“propertyTwo”的内容选择每个列表框-项的位置 我研究了与我的情况类似的示例,使用了Collect
observedcollection
,它填充在我的ViewModel
构造函数中。此ObservableCollection
包含一个具有两个属性(两个字符串)的自定义对象
当前,XAML/View对应项拥有两个单独的列表框,它们都绑定到一个数据模板
,该模板选择要在列表框中显示为条目的属性。在本例中,它显示“propertyOne”
是否可以有一个数据模板
,它可以根据“propertyTwo”的内容选择每个列表框
-项的位置
我研究了与我的情况类似的示例,使用了CollectionViewSource
,但我不太确定如何在我的项目中实现这一点,因为我对使用WPF和遵循MVVM结构相当陌生。这是否涉及在视图后面的代码中创建过滤器事件
下面列出的是我的代码片段,我认为这对理解我的问题很有用。如果您能帮助解决此问题,我们将不胜感激
看法
确保我收到了你的问题-你想让第二个列表框显示属性吗?我想你需要重新表述这个问题。甚至可以添加一个具有所需输出的示例。否。列表框仍将显示propertyOne,但由propertyTwo决定它指向哪个“列表框”。e、 g.如果自定义对象的字符串“read”设置为“propertyTwo”,则该对象(其propertyOne显示在“ListBox”中)将被发送到“ListBoxTwo”。在纯xaml中,我唯一能想到的是将可见性设置为使用datatrigger折叠的样式。。。因此,您将所有内容添加到2个列表框中,但容器只是折叠起来。除此之外,您还需要在后面编写一些代码1。CustomObjectClass
是可变的(即setter是公共的,类实现了INotifyPropertyChanged
接口)?2.使用的是哪个版本的WPF?为第二个列表添加另一个模板,像这样,并在DataTrigger中交换值。尝试了这个,它似乎可以工作。据我所知,这将基本上隐藏value=
旁边指示的值。
<Window.Resources>
<DataTemplate x:Key="ListBoxTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=propertyOne}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListBox x:Name="ListBoxOne"
Height="Auto"
Width="Auto"
ItemsSource="{Binding TestCollection}"
ItemTemplate="{StaticResource ListBoxTemplate}" />
<ListBox x:Name="ListBoxTwo"
Height="Auto"
Width="Auto"
ItemsSource="{Binding TestCollection}"
ItemTemplate="{StaticResource ListBoxTemplate}" />
public class ViewModel
{
public ObservableCollection<Item> TestCollection { get; set; }
public ViewModel()
{
//populates the collection from an XML file
//with propertyOne & propertyTwo for each item
TestCollection = CustomObjectClass.DeserializeToColl<Item>("path");
}
}
public class CustomObjectClass
{
public string propertyOne { get; set; }
public string propertyTwo { get; set; }
}
<DataTemplate x:Key="ListBoxTemplate">
<StackPanel>
<StackPanel.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=propertyTwo}" Value="read">
<Setter Property="StackPanel.Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock Text="{Binding Path=propertyOne}" />
</StackPanel>
</DataTemplate>