WPF Grid 数据绑定,当数据源发生变化后控件值随之更新

前台页面:

<Window x:Class="WPFTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid  Name="gdTest">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>

        <StackPanel  Grid.Row="0" Orientation="Horizontal" >
            <TextBlock  Width="80" TextAlignment="Right" VerticalAlignment="Center" >测试1:</TextBlock>
            <TextBox Width="100" VerticalAlignment="Center"  Text="{Binding Path=Test1}" Name="txtTest1" ></TextBox>
          
        </StackPanel>
        <StackPanel  Grid.Row="1" Orientation="Horizontal" >
            <TextBlock  Width="80" TextAlignment="Right" VerticalAlignment="Center" >测试2:</TextBlock>
        <TextBox Width="100" VerticalAlignment="Center"  Text="{Binding Path=Test2}" Name="txtTest2"  ></TextBox>
        </StackPanel>

        <StackPanel  Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" >
            <Button Height="30" Width="80" Name="btnTest" Click="btnTest_Click" Margin="0,0,5,0" >确定</Button>
        </StackPanel>
    </Grid>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
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 WPFTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }
        public Tset test = new Tset();
        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            test.Test1 = "111111";
            test.Test2 = "222222";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            gdTest.DataContext = test;
        }
    }
}

绑定的实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WPFTest
{
    public class Tset : INotifyPropertyChanged
    {

        [field: NonSerializedAttribute()]
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        private string _test1;
        public string Test1
        {
            get
            {
                return _test1;
            }
            set
            {
                if (_test1 != value)
                {
                    _test1 = value;
                    NotifyPropertyChanged("Test1");
                }
            }
        }
        public string Test2 { get; set; }
    }
}

 

转载于:https://www.cnblogs.com/tong467/archive/2013/03/21/2973399.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值