# wpf 数据绑定 列表 ObservableCollection
PrismDryIocDemo\PrismDryIocDemo\App.xaml
| |
| <Application x:Class="PrismDryIocDemo.App" |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| xmlns:local="clr-namespace:PrismDryIocDemo" |
| StartupUri="MainWindow.xaml"> |
| <Application.Resources> |
| |
| </Application.Resources> |
| </Application> |
| |
PrismDryIocDemo\PrismDryIocDemo\App.xaml.cs
| |
| using System.Configuration; |
| using System.Data; |
| using System.Windows; |
| |
| namespace PrismDryIocDemo |
| { |
| |
| |
| |
| public partial class App : Application |
| { |
| } |
| |
| } |
| |
PrismDryIocDemo\PrismDryIocDemo\AssemblyInfo.cs
| |
| using System.Windows; |
| |
| [assembly: ThemeInfo( |
| ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located |
| //(used if a resource is not found in the page, |
| // or application resource dictionaries) |
| ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located |
| //(used if a resource is not found in the page, |
| // app, or any theme specific resource dictionaries) |
| )] |
| |
PrismDryIocDemo\PrismDryIocDemo\MainWindow.xaml
| |
| <Window x:Class="PrismDryIocDemo.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" |
| xmlns:local="clr-namespace:PrismDryIocDemo" xmlns:viewmodels="clr-namespace:PrismDryIocDemo.ViewModels" |
| mc:Ignorable="d" |
| Title="MainWindow" Height="450" Width="800"> |
| |
| 👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇 |
| <Window.DataContext> |
| <viewmodels:MainViewModel /> |
| </Window.DataContext> |
| |
| <Grid> |
| |
| <Grid.ColumnDefinitions > |
| <ColumnDefinition Width="320"></ColumnDefinition> |
| <ColumnDefinition></ColumnDefinition> |
| </Grid.ColumnDefinitions> |
| |
| |
| 👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇 |
| |
| <ListBox ItemsSource="{Binding ListItems }"> |
| <ListBox.ItemTemplate> |
| <DataTemplate> |
| <Grid > |
| |
| <Grid.ColumnDefinitions > |
| <ColumnDefinition Width="100"></ColumnDefinition> |
| <ColumnDefinition Width="100"></ColumnDefinition> |
| <ColumnDefinition Width="100"></ColumnDefinition> |
| </Grid.ColumnDefinitions> |
| |
| <TextBlock Text="{Binding Name}" Background="Pink" Margin="5 0 5 0" Grid.Column="0"></TextBlock> |
| <TextBlock Text="{Binding Age}" Background="Pink" Margin="5 0 5 0" Grid.Column="1"></TextBlock> |
| <TextBlock Text="{Binding Sex}" Background="Pink" Margin="5 0 5 0" Grid.Column="2"></TextBlock> |
| </Grid> |
| |
| </DataTemplate> |
| </ListBox.ItemTemplate> |
| </ListBox> |
| |
| </Grid> |
| </Window> |
| |
PrismDryIocDemo\PrismDryIocDemo\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 PrismDryIocDemo |
| { |
| |
| |
| |
| public partial class MainWindow : Window |
| { |
| public MainWindow() |
| { |
| InitializeComponent(); |
| } |
| } |
| } |
PrismDryIocDemo\PrismDryIocDemo\PrismDryIocDemo.csproj
| |
| <Project Sdk="Microsoft.NET.Sdk"> |
| |
| <PropertyGroup> |
| <OutputType>WinExe</OutputType> |
| <TargetFramework>net6.0-windows</TargetFramework> |
| <Nullable>enable</Nullable> |
| <ImplicitUsings>enable</ImplicitUsings> |
| <UseWPF>true</UseWPF> |
| </PropertyGroup> |
| |
| <ItemGroup> |
| <PackageReference Include="Prism.DryIoc" Version="9.0.537" /> |
| </ItemGroup> |
| |
| </Project> |
| |
PrismDryIocDemo\PrismDryIocDemo\PrismDryIocDemo.csproj.user
| |
| <?xml version="1.0" encoding="utf-8"?> |
| <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
| <PropertyGroup /> |
| <ItemGroup> |
| <ApplicationDefinition Update="App.xaml"> |
| <SubType>Designer</SubType> |
| </ApplicationDefinition> |
| </ItemGroup> |
| <ItemGroup> |
| <Page Update="MainWindow.xaml"> |
| <SubType>Designer</SubType> |
| </Page> |
| </ItemGroup> |
| </Project> |
| |
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Text; |
| using System.Threading.Tasks; |
| |
| namespace PrismDryIocDemo.Common |
| { |
| public class Student |
| { |
| public string Name { get; set; } |
| public string Sex { get; set; } |
| public int Age { get; set; } |
| } |
| } |
| |
PrismDryIocDemo\PrismDryIocDemo\ViewModels\MainViewModel.cs
| |
| using PrismDryIocDemo.Common; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.ObjectModel; |
| using System.Linq; |
| using System.Text; |
| using System.Threading.Tasks; |
| |
| namespace PrismDryIocDemo.ViewModels |
| { |
| |
| public class MainViewModel |
| { |
| |
| 👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇 |
| private ObservableCollection<Student> listItems; |
| public ObservableCollection<Student> ListItems { get => listItems; set => listItems = value; } |
| |
| |
| public MainViewModel() |
| { |
| this.ListItems = new ObservableCollection<Student>(); |
| this.ListItems.Add(new Student() { Name = "Alice",Sex = "M",Age=19}); |
| this.ListItems.Add(new Student() { Name = "Brue",Sex = "F",Age=10}); |
| this.ListItems.Add(new Student() { Name = "Celina",Sex = "M",Age=18}); |
| this.ListItems.Add(new Student() { Name = "Elie",Sex = "M",Age=18}); |
| this.ListItems.Add(new Student() { Name = "Felica",Sex = "M",Age=18}); |
| this.ListItems.Add(new Student() { Name = "Sara",Sex = "M",Age=18}); |
| } |
| |
| |
| } |
| |
| } |
| |
| |
PrismDryIocDemo\PrismDryIocDemo.sln
| |
| |
| Microsoft Visual Studio Solution File, Format Version 12.00 |
| # Visual Studio Version 17 |
| VisualStudioVersion = 17.11.35219.272 |
| MinimumVisualStudioVersion = 10.0.40219.1 |
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrismDryIocDemo", "PrismDryIocDemo\PrismDryIocDemo.csproj", "{EC60CBE3-839D-45B7-A6D6-8FFE8FBB96FD}" |
| EndProject |
| Global |
| GlobalSection(SolutionConfigurationPlatforms) = preSolution |
| Debug|Any CPU = Debug|Any CPU |
| Release|Any CPU = Release|Any CPU |
| EndGlobalSection |
| GlobalSection(ProjectConfigurationPlatforms) = postSolution |
| {EC60CBE3-839D-45B7-A6D6-8FFE8FBB96FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
| {EC60CBE3-839D-45B7-A6D6-8FFE8FBB96FD}.Debug|Any CPU.Build.0 = Debug|Any CPU |
| {EC60CBE3-839D-45B7-A6D6-8FFE8FBB96FD}.Release|Any CPU.ActiveCfg = Release|Any CPU |
| {EC60CBE3-839D-45B7-A6D6-8FFE8FBB96FD}.Release|Any CPU.Build.0 = Release|Any CPU |
| EndGlobalSection |
| GlobalSection(SolutionProperties) = preSolution |
| HideSolutionNode = FALSE |
| EndGlobalSection |
| GlobalSection(ExtensibilityGlobals) = postSolution |
| SolutionGuid = {9D809D94-E0E4-47CD-A140-EBC92C3A22E4} |
| EndGlobalSection |
| EndGlobal |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战