后台修改数据前台不刷新可能的原因:
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));
}
}