wpf mvvm List
WPFListView\ListApp\App.xaml
<Application x:Class="ListApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
WPFListView\ListApp\App.xaml.cs
using System.Configuration;
using System.Data;
using System.Windows;
namespace ListApp;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
WPFListView\ListApp\ListApp.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
WPFListView\ListApp\MainWindow.xaml
<Window x:Class="ListApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- 标题区域 -->
<TextBlock Text="人员信息列表" FontSize="24" FontWeight="Bold"
HorizontalAlignment="Center" Margin="0,10,0,10"/>
<!-- 列表展示区域 -->
<ListBox ItemsSource="{Binding People}"
SelectedItem="{Binding SelectedPerson}"
Grid.Row="1" Margin="20">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="{Binding Name}" Width="120" FontWeight="Bold"/>
<TextBlock Text="{Binding Age}" Width="60"/>
<TextBlock Text="{Binding Email}" Width="200"/>
<TextBlock Text="{Binding Phone}" Width="120"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
WPFListView\ListApp\MainWindow.xaml.cs
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListApp;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new PersonViewModel();
}
}
WPFListView\ListApp\Person.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class Person : INotifyPropertyChanged
{
private string _name;
private int _age;
private string _email;
private string _phone;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
public int Age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged();
}
}
public string Email
{
get { return _email; }
set
{
_email = value;
OnPropertyChanged();
}
}
public string Phone
{
get { return _phone; }
set
{
_phone = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
WPFListView\ListApp\PersonViewModel.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class PersonViewModel : INotifyPropertyChanged
{
private ObservableCollection<Person> _people;
private Person _selectedPerson;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<Person> People
{
get { return _people; }
set
{
_people = value;
OnPropertyChanged();
}
}
public Person SelectedPerson
{
get { return _selectedPerson; }
set
{
_selectedPerson = value;
OnPropertyChanged();
}
}
public PersonViewModel()
{
// 初始化人员数据
People = new ObservableCollection<Person>
{
new Person
{
Name = "张三",
Age = 25,
Email = "zhangsan@example.com",
Phone = "13800138000"
},
new Person
{
Name = "李四",
Age = 30,
Email = "lisi@example.com",
Phone = "13900139000"
},
new Person
{
Name = "王五",
Age = 22,
Email = "wangwu@example.com",
Phone = "13700137000"
},
new Person
{
Name = "赵六",
Age = 35,
Email = "zhaoliu@example.com",
Phone = "13600136000"
},
new Person
{
Name = "钱七",
Age = 28,
Email = "qianqi@example.com",
Phone = "13500135000"
}
};
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
WPFListView\build.bat
dotnet build
WPFListView\run.bat
dotnet run --project ListApp
WPFListView\WPFListView.sln
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListApp", "ListApp\ListApp.csproj", "{69B64D51-DA82-469F-A2D0-BA55E67E68E6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Debug|x64.ActiveCfg = Debug|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Debug|x64.Build.0 = Debug|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Debug|x86.ActiveCfg = Debug|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Debug|x86.Build.0 = Debug|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Release|Any CPU.Build.0 = Release|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Release|x64.ActiveCfg = Release|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Release|x64.Build.0 = Release|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Release|x86.ActiveCfg = Release|Any CPU
{69B64D51-DA82-469F-A2D0-BA55E67E68E6}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal