WPF DataGrid 数据不刷新问题

后台修改数据前台不刷新可能的原因:

1.前台页面没有正确Binding

2.后台数据定义没有使用依赖属性:DependencyObject,导致数据更改界面无法收到通知。

3.数据容器没有使用ObservableCollection,导致容器增删界面不响应。

前台代码:

<Window x:Class="DepwndencyProp.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:DepwndencyProp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="5*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DataGrid x:Name="dataGrid1" AutoGenerateColumns="False" Grid.Row="0" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="propty" Width=" 100" Binding="{Binding MyProperty ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn Header="propty1" Width=" 100" Binding="{Binding MyProperty1 ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="添加"  Margin="10 " Click="Button_Click">
            </Button>
            <TextBox Width="100" Height="40" Name="textBox"/>
            <Button Content="修改"  Margin="10 " Click="Button_Click_1">
            </Button>
        </StackPanel>
        
            
        
    </Grid>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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;

namespace DepwndencyProp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<TestClass> testClasses;
        public MainWindow()
        {
            InitializeComponent();
            testClasses = new ObservableCollection<TestClass>();
            dataGrid1.ItemsSource = testClasses;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            testClasses.Add(new TestClass { MyProperty = "1", MyProperty1="2" });
            //dataGrid1.ItemsSource = null;
            //dataGrid1.ItemsSource = testClasses;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (testClasses.Count>0)
            {

                testClasses[Convert.ToInt32(textBox.Text)].MyProperty = "2"; 
            }
            
        }
    }

    public class TestClass:DependencyObject
    {


        public string  MyProperty
        {
            get { return (string )GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(string ), typeof(TestClass), new PropertyMetadata(null));



        public string MyProperty1
        {
            get { return (string)GetValue(MyProperty1Property); }
            set { SetValue(MyProperty1Property, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyProperty1Property =
            DependencyProperty.Register("MyProperty1", typeof(string), typeof(TestClass), new PropertyMetadata(null));



    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值