制作一个这样的wpf窗口布局是grid由两行组成。第一行是一个textbox + button 第二行是一个treeview 这个treeview由两层组成 第一层是班级号第二层是学生姓名组成的列表支持单击班级后把所有学生折叠起来。在textbox输入关键词进行搜索则treeview会显示每个班级中姓名含有该关键词的所有学生。要求treeview的itemsource是由学生组成的列表每个学生包含
以下是使用MVVM架构的WPF窗口的完整代码:
MainWindow.xaml:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBox Text="{Binding SearchKeyword}" Width="200" Margin="10" />
<Button Content="Search" Command="{Binding SearchCommand}" Margin="10" />
</StackPanel>
<TreeView Grid.Row="1" ItemsSource="{Binding Students}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Students}">
<TextBlock Text="{Binding ClassNumber}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
MainViewModel.cs:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace WpfApp
{
public class MainViewModel : INotifyPropertyChanged
{
private string _searchKeyword;
private List<Student> _allStudents;
private ObservableCollection<Student> _students;
public MainViewModel()
{
// 假设有以下班级和学生数据
_allStudents = new List<Student>
{
new Student { ClassNumber = "1", Name = "Alice" },
new Student { ClassNumber = "1", Name = "Bob" },
new Student { ClassNumber = "2", Name = "Charlie" },
new Student { ClassNumber = "2", Name = "Dave" },
new Student { ClassNumber = "3", Name = "Eve" },
new Student { ClassNumber = "3", Name = "Frank" }
};
Students = new ObservableCollection<Student>(_allStudents);
SearchCommand = new RelayCommand(Search);
}
public string SearchKeyword
{
get => _searchKeyword;
set
{
_searchKeyword = value;
OnPropertyChanged();
}
}
public ObservableCollection<Student> Students
{
get => _students;
set
{
_students = value;
OnPropertyChanged();
}
}
public ICommand SearchCommand { get; }
private void Search()
{
if (string.IsNullOrEmpty(SearchKeyword))
{
Students = new ObservableCollection<Student>(_allStudents);
}
else
{
var filteredStudents = _allStudents.Where(s => s.Name.Contains(SearchKeyword)).ToList();
Students = new ObservableCollection<Student>(filteredStudents);
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
Student.cs:
namespace WpfApp
{
public class Student
{
public string ClassNumber { get; set; }
public string Name { get; set; }
}
}
RelayCommand.cs(可选,用于实现命令):
using System;
using System.Windows.Input;
namespace WpfApp
{
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public RelayCommand(Action execute, Func<bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute?.Invoke() ?? true;
}
public void Execute(object parameter)
{
_execute();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
这是一个简单的使用MVVM架构的WPF窗口,其中使用了RelayCommand来实现命令,MainViewModel作为视图模型处理用户输入和数据操作,Student作为数据模型表示学生对象
原文地址: https://www.cveoy.top/t/topic/iVsB 著作权归作者所有。请勿转载和采集!