问题描述
我已经设置了一个绑定到视图模型的简单 WPF ListView,ListView 使用 CollectionViewSource 根据各种属性对项目进行排序以动态创建 ListView 层次结构 - 这很好用。
我还添加了一个过滤器以允许例如搜索 ListView - 这也很有效。
我遇到的问题是,当用户在搜索框中键入一些文本时,搜索工作,但它关闭了所有扩展器。
这对用户来说很烦人,有什么办法可以在搜索过程中和搜索后保持扩展器状态不变?
WPF 例程将狗分成不同的组。
狗类:
public partial class Dogs : viewmodelBase
{
//Group one nesting level
private string _l1grouping;
public string L1grouping
{
get
{
return _l1grouping;
}
set
{
_l1grouping = value;
OnPropertyChanged(nameof(L1grouping));
}
}
//Group two nesting level
private string _l2grouping;
public string L2grouping
{
get
{
return _l2grouping;
}
set
{
_l2grouping = value;
OnPropertyChanged(nameof(L2grouping));
}
}
//Group three nesting level
private string _l3grouping;
public string L3grouping
{
get
{
return _l3grouping;
}
set
{
_l3grouping = value;
OnPropertyChanged(nameof(L3grouping));
}
}
//Name
private string _name;
public string Name
{
get => _name;
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged(nameof(Name));
}
}
}
}
查看模型类:
class MainWindowviewmodel : viewmodelBase
{
public ICollectionView DogsCollectionView { get; }
private ObservableCollection<Dogs> _dogs = new ObservableCollection<Dogs>();
public ObservableCollection<Dogs> Dogs
{
get => _dogs;
set
{
if (value != _dogs)
{
_dogs = value;
OnPropertyChanged(nameof(Dogs));
}
}
}
private string _dogFilter = string.Empty;
public string DogFilter
{
get
{
return _dogFilter;
}
set
{
_dogFilter = value;
OnPropertyChanged(nameof(DogFilter));
DogsCollectionView.Refresh();
}
}
public MainWindowviewmodel()
//Constructor
{
DogsCollectionView = CollectionViewSource.Getdefaultview(_dogs);
//Set up filter
DogsCollectionView.Filter = FilterbrowserItems;
//Set up grouping
DogsCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SaveExpanderState.Dogs.L1grouping)));
DogsCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SaveExpanderState.Dogs.L2grouping)));
DogsCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SaveExpanderState.Dogs.L3grouping)));
}
private bool FilterbrowserItems(object obj)
{
if (obj is Dogs check)
{
string nametocheck = check.Name.ToLower();
return nametocheck.Contains(DogFilter.ToLower());
}
return false;
}
}
创建模型的代码:
public partial class MainWindow : Window
{
MainWindowviewmodel Dogviewmodel
{
get;
set;
}
public MainWindow()
{
InitializeComponent();
Dogviewmodel = new MainWindowviewmodel();
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Max",L1grouping = "Large",L2grouping = "brown",L3grouping = "Fast" }); ;
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Bob",L1grouping = "Small",L3grouping = "Slow" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Fido",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Brian",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Steve",L2grouping = "Black",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "emma",L3grouping = "Slow" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Shep",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Lassy",L2grouping = "White",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Burt",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Siggy",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Heidi",L2grouping = "Black and White",L3grouping = "Fast" });
Dogviewmodel.Dogs.Add(new Dogs() { Name = "Loki",L2grouping = "Yellow",L3grouping = "Fast" });
ListViewDogs.DataContext = Dogviewmodel;
FilterBoxText.DataContext = Dogviewmodel;
}
}
和用于窗口的 XAML:
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal"
Margin="10">
<TextBlock Text="Filter: " />
<TextBox x:Name="FilterBoxText"
Width="330"
Text="{Binding DogFilter,UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</StackPanel>
<ListView Name="ListViewDogs"
Margin="10,10,10"
Height="370"
ItemsSource="{Binding DogsCollectionView}"
IsTextSearchEnabled="False"
IsSynchronizedWithCurrentItem="True">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style targettype="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander>
<Expander.Header>
<TextBlock Text="{Binding Name}"/>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</StackPanel>
</Grid>
谢谢,我希望得到一些建议。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)