wpf 简单实验 数据更新 列表更新

1.概要

1.1 需求

一个列表提供添加修改删除的功能,添加和修改的内容都来自一个输入框

1.2 要点

  • DisplayMemberPath="Zhi"
  • 列表.ItemsSource = datalist;
  • (列表.SelectedItem != null)
  • (列表.SelectedItem as A).Zhi = 内容.Text;
  • datalist.Remove((列表.SelectedItem as A)!);
  • public String? Zhi { get { return _zhi; } set { _zhi = value; OnPropertyChanged(nameof(Zhi)); } }
  • PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));

2.代码

2.1 xaml

<Window x:Class="WpfApp5.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:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <TextBox Name="内容" ></TextBox>
        <Button Name="Add" Content="添加" Click="Add_Click" />
        <Button Name="Update" Content="修改" Click="Update_Click" />
        <Button Name="Delete" Content="删除" Click="Delete_Click" />
        <ListBox Name="列表" DisplayMemberPath="Zhi" Height="100"></ListBox>
    </StackPanel>
</Window>

2.2 代码

using System.Collections.ObjectModel;
using System.ComponentModel;
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 WpfApp5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// 连表数据绑定
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<A> datalist = new ObservableCollection<A>();
        //List<A> datalist = new List<A>();

        public MainWindow()
        {
            InitializeComponent();
            init();
        }
        private void init()
        {
            datalist.Add(new A { Zhi = "1" });
            datalist.Add(new A { Zhi = "2" });
            datalist.Add(new A { Zhi = "3" });
            //列表.DataContext = datalist;
            列表.ItemsSource = datalist;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            datalist.Add(new A { Zhi = 内容.Text });
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
            if (列表.SelectedItem != null)
            {
                (列表.SelectedItem as A).Zhi = 内容.Text;
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (列表.SelectedItem != null)
            {
                datalist.Remove((列表.SelectedItem as A)!);
            }
        }
    }
    class A:INotifyPropertyChanged
    {
        private String? _zhi;
        public String? Zhi { get { return _zhi; } set { _zhi = value; OnPropertyChanged(nameof(Zhi)); } }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged(String name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

3.运行结果

相关推荐
~央千澈~9 小时前
优雅草央千澈-关于蓝湖如何快速的标注交互原型是如何使用的-如何使用蓝湖设计交互原型和整个软件项目的流程逻辑-实践项目详细说明
ui·交互·蓝湖
明耀13 小时前
WPF TabControl 设置item不能点击
wpf
军训猫猫头14 小时前
20.抽卡只有金,带保底(WPF) C#
ui·c#·wpf
明耀14 小时前
WPF 设置平均布局 如果隐藏的话,能够自动扩展
wpf
wuningw15 小时前
ant-design-ui的Select选择器多选时同时获取label与vaule值
ui·arcgis
SoraLuna20 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
晚安苏州1 天前
WPF DataTemplate 数据模板
wpf
晓纪同学1 天前
QT创建一个模板槽和信号刷新UI
开发语言·qt·ui
甜甜不吃芥末2 天前
WPF依赖属性详解
wpf
程序视点2 天前
【安全漏洞】Vue UI库Vant组件遭恶意投毒,字节RspacK也中招!请紧急修复!
前端·vue.js·ui