wpf数据绑定、变更通知

首先是一个支持数据变更通知的类
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;

namespace EtoonProduct.exeWpf
{
    [Serializable]
    
///需要实现INotifyPropertyChanged接口
    public class DjStatisticHistory : INotifyPropertyChanged
    {
        
int coID;
        
public int CoID
        {
            
get { return coID; }
            
set
            {
                coID 
= value;

                
//通知属性变更
                Notify("CoID");
            }
        }

        
string url;
        
public string URL
        {
            
get { return url; }
            
set
            {
                url 
= value;

                
//通知属性变更
                Notify("URL");
            }
        }

        
public DjStatisticHistory(int coID, string url)
        {
            
this.coID = coID;
            
this.url = url;
        }

        
#region INotifyPropertyChanged 成员

        
/// <summary>
        
/// 属性变化通知委托
        
/// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        
/// <summary>
        
/// 实现INotifyPropertyChanged接口方法。这个方法实现比较固定,直接使用就可以了
        
/// </summary>
        
/// <param name="propName"></param>
        protected void Notify(string propName)
        {
            
if (this.PropertyChanged != null)
            {
                PropertyChanged(
thisnew PropertyChangedEventArgs(propName));
            }
        }

        
#endregion
    }
}


Window1.xaml.cs

代码
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;

using System.ComponentModel;

namespace EtoonProduct.exeWpf
{
    
/// <summary>
    
/// Window1.xaml 的交互逻辑
    
/// </summary>
    public partial class Window1 : Window
    {
        DjStatisticHistory sta 
= new DjStatisticHistory(201"http://localhost/dj/web");

        
public Window1()
        {
            InitializeComponent();

            dataBind();
        }

        
private void dataBind()
        {
            txtURL.Text 
= sta.URL;
            txtCoID.Text 
= sta.CoID.ToString();

            
///订阅属性变更
            sta.PropertyChanged += new PropertyChangedEventHandler(sta_PropertyChanged);

            urlView();
        }

        
/// <summary>
        
/// 实现属性变更逻辑
        
/// </summary>
        void sta_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            
switch (e.PropertyName)
            { 
                
case "CoID" :
                    txtCoID.Text 
= sta.CoID.ToString();
                    
break;
                
case "URL":
                    txtURL.Text 
= sta.URL;
                    
break;
            }
            urlView();
        }

        
private void urlView()
        {
            labPage.Content 
= sta.URL + "/PriceSystem/Statistic/Static_HitoryMake.aspx?CoID=" + sta.CoID;
        }

        
private void txtURL_TextChanged(object sender, TextChangedEventArgs e)
        {
            sta.URL 
= txtURL.Text;
        }

        
private void txtCoID_TextChanged(object sender, TextChangedEventArgs e)
        {
            sta.CoID 
= Convert.ToInt32(txtCoID.Text);
        }
    }    
}

 

 

window1.xaml

代码
<Window x:Class="EtoonProduct.exeWpf.Window1"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Title
="Window1" Height="201" Width="566">
    
<Grid>
        
<Button Height="24" HorizontalAlignment="Left" Margin="76,83,0,0" Name="button1" VerticalAlignment="Top" Width="105">保存设置</Button>
        
<TextBox Height="23" Margin="76,41,174,0" Name="txtCoID" VerticalAlignment="Top" TextChanged="txtCoID_TextChanged">201</TextBox>
        
<Label Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" Width="60">虚拟目录</Label>
        
<Label Margin="12,127,12,0" Name="labPage" Height="28" VerticalAlignment="Top">Static_HitoryMake.aspx?CoID=</Label>
        
<Label Height="28" HorizontalAlignment="Left" Margin="10,41,0,0" Name="label3" VerticalAlignment="Top" Width="64">公司ID</Label>
        
<TextBox Height="23" Margin="76,12,174,0" Name="txtURL" VerticalAlignment="Top" TextChanged="txtURL_TextChanged">http://localhost/dj/web</TextBox>
    </Grid>
</Window>

 

 

posted on 2009-12-29 14:45  一粒沙  阅读(1649)  评论(0编辑  收藏  举报