WPF 更改数据源时通知表格 更改List通知DataGrid

用到集合ObservableCollection,此集合微软的解释为:动态收集数据并通知到显示的控件,无论数据是刷新、删除、增加、更新。

新建Model,注意看里面的大小写。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WPFTest
{
    //绑定的类必须要继承自INotifyPropertyChanged
    public class ViewModelTest: INotifyPropertyChanged
    {
        private string testString = "aaaaaa";

        public ObservableCollection<myString> randomString = new ObservableCollection<myString>()
        {
           new myString{ randomStringValue="bbbbbbbbbbb"},
        };
        //ObservableCollection相当于一种List,增删改可以通知到界面。
        public ObservableCollection<myString> RandomString
        {
            get => randomString;
            set => SetProperty(ref randomString, value);
        }

        public string TestString
        {
            get => testString;
            set => SetProperty(ref testString, value);
        }

        public class myString
        { 
            public string randomStringValue { set; get; }
        }

        protected void SetProperty<T>(ref T prop, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(prop, value) == false)
            {
                prop = value;
                OnPropertyChanged(propertyName);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(String info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

界面

<Window x:Class="WPFTest.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:WPFTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="688.091" Width="1174.727" Background="LightBlue">
    <Grid>
        <Label Content="{Binding TestString}" 
               HorizontalAlignment="Left" 
               Height="29" Margin="204,116,0,0" Background="LightCyan" 
               VerticalAlignment="Top" Width="460"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="43" 
                Margin="204,206,0,0" VerticalAlignment="Top" Width="144" Click="Button_Click"/>
        <Label Content=""  x:Name="label2"
            HorizontalAlignment="Left" 
            Height="29" Margin="204,163,0,0" Background="LightCyan" 
            VerticalAlignment="Top" Width="460"/>
        <TextBox HorizontalAlignment="Left" Height="36" Margin="204,75,0,0" 
                 TextWrapping="Wrap" Text="{Binding TestString,Mode=TwoWay}" 
                 VerticalAlignment="Top" Width="460"/>
        <Button Content="Button" HorizontalAlignment="Left" 
                Height="45" Margin="204,500,0,0" VerticalAlignment="Top" Width="169" Click="Button_Click_1"/>
        <DataGrid HorizontalAlignment="Left" x:Name="dataGrid1"
                  ItemsSource="{Binding RandomString}" AutoGenerateColumns="False"
                  Height="178" Margin="204,290,0,0" VerticalAlignment="Top" Width="688">
            <DataGrid.Columns>
                <DataGridTextColumn Header="randomStringValue" Binding="{Binding randomStringValue}" />
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

后端代码

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using JSON=System.Text.Json.JsonSerializer;

namespace WPFTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ViewModelTest vm = new ViewModelTest();
        public MainWindow()
        {
            InitializeComponent();
            DataContext= vm;//必须要在此处指定DataContext,不指定则为当前类
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            vm.TestString = Guid.NewGuid().ToString();
            label2.Content = vm.TestString;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            vm.randomString.Add(
                new ViewModelTest.myString 
                {
                    randomStringValue=Guid.NewGuid().ToString()
                }
                );
        }
        public class Number
        {
            public string randomStringValue { set; get; }
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值