WPF 数据绑定,界面刷新的两种方法-----INotifyPropertyChanged
.Netformwork4.0及以下版本 -------INotifyPropertyChanged
命名空间: System.ComponentModel
后台代码
public partial class DvrWnd : UserControl
{
public DvrWnd()
{
InitializeComponent();
}
private void InitInfo()
{
for (int i = 0; i < 10; i++)
{
DvrInfo dvrInfo = new DvrInfo();
dvrInfo.strDvrName = "编码器" + i.ToString();
dvrInfo.strDvrIp = "10.10.13.10" + i.ToString();
dvrInfo.strDvrPort = "506" + i.ToString();
dvrInfo.strDvrType = "Type " + i.ToString();
dvrInfo.strDvrCode = "340201030002150000" + i.ToString();
dvrInfoList.Items.Add(dvrInfo);
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
dvrInfoList.Items.Clear();
InitInfo();
}
//编码器信息, 只要改了类中的属性, 就能随便用
public class DvrInfo : INotifyPropertyChanged
{
private string m_strDvrIp = string.Empty;
private string m_strDvrPort = string.Empty;
private string m_strDvrName = string.Empty;
private string m_strDvrType = string.Empty;
private string m_strDvrCode = string.Empty;
public string strDvrIp
{
set
{
if (m_strDvrIp != value)
{
m_strDvrIp = value;
}
OnPropertyChanged("strDvrIp");
}
get
{
return m_strDvrIp;
}
}
public string strDvrPort
{
set
{
if (m_strDvrPort != value)
{
m_strDvrPort = value;
OnPropertyChanged("strDvrPort");
}
}
get
{
return m_strDvrPort;
}
}
public string strDvrName
{
set
{
if (m_strDvrName != value)
{
m_strDvrName = value;
OnPropertyChanged("strDvrName");
}
}
get
{
return m_strDvrName;
}
}
public string strDvrType
{
set
{
if (m_strDvrType != value)
{
m_strDvrType = value;
OnPropertyChanged("strDvrType");
}
}
get
{
return m_strDvrType;
}
}
public string strDvrCode
{
set
{
if (m_strDvrCode != value)
{
m_strDvrCode = value;
OnPropertyChanged("strDvrCode");
}
}
get
{
return m_strDvrCode;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string strPropertyInfo)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(strPropertyInfo));
}
}
}
前端代码绑定
<UserControl x:Class="testDevice.DvrWnd" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Background="White" Loaded="UserControl_Loaded" > <Grid> <DataGrid Name="dvrInfoList" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="编码器名" IsReadOnly="True" Width="2*" Binding ="{Binding strDvrName}" />
//红色标记部分为OnPropertyChanged("strDvrName");中strDvrName, 并不是DvrInfo.strDvrName中的strDvrName <DataGridTextColumn Header="编码器IP" Width="*" IsReadOnly="True" Binding="{Binding strDvrIp}"/> <DataGridTextColumn Header="编码器Port" Width="*" IsReadOnly="True" Binding="{Binding strDvrPort}"/> <DataGridTextColumn Header="编码器类型" Width="*" IsReadOnly="True" Binding="{Binding strDvrType}"/> <DataGridTextColumn Header="编码器编码" Width="2*" IsReadOnly="True" Binding="{Binding strDvrCode}" /> </DataGrid.Columns> </DataGrid> </Grid> </UserControl>
.Netformwork4.5及以上版本 -------INotifyPropertyChanged,CallerMemberName
命名空间: System.ComponentModel
System.Runtime.CompilerServices
后台代码:
public CvrWnd()
{
InitializeComponent();
}
private void InitInfo()
{
for (int i = 0; i < 10; i++)
{
CvrInfo cvrInfo = new CvrInfo();
cvrInfo.strCvrName = "磁盘阵列" + i.ToString();
cvrInfo.strCvrIP = "10.10.13.10" + i.ToString();
cvrInfo.strCvrPort = "506" + i.ToString();
cvrInfo.strCvrType = "Type " + i.ToString();
cvrInfo.strCvrCode = "340201030002150000" + i.ToString();
cvrInfoList.Items.Add(cvrInfo);
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
cvrInfoList.Items.Clear();
InitInfo();
}
//磁盘阵列信息
public class CvrInfo : INotifyPropertyChanged
{
private string m_strCvrIP = string.Empty;
private string m_strCvrPort = string.Empty;
private string m_strCvrName = string.Empty;
private string m_strCvrType = string.Empty;
private string m_strCvrCode = string.Empty;
public string strCvrIP
{
set
{
UpdateProperty(ref m_strCvrIP, value);
}
get
{
return m_strCvrIP;
}
}
public string strCvrPort
{
set
{
UpdateProperty(ref m_strCvrPort, value);
}
get
{
return m_strCvrPort;
}
}
public string strCvrName
{
set
{
UpdateProperty(ref m_strCvrName, value);
}
get
{
return m_strCvrName;
}
}
public string strCvrType
{
set
{
UpdateProperty(ref m_strCvrType, value);
}
get
{
return m_strCvrType;
}
}
public string strCvrCode
{
set
{
UpdateProperty(ref m_strCvrCode, value);
}
get
{
return m_strCvrCode;
}
}
private void UpdateProperty<T>(ref T properValue, T newValue, [CallerMemberName] string propertyName = "")
{
if (object.Equals(properValue, newValue))
{
return;
}
properValue = newValue;
OnPropertyChanged(propertyName);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
前台代码:
<Grid> <DataGrid Name="cvrInfoList" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="磁盘阵列名" Width="*" Binding="{Binding strCvrName}" />
//strCvrName 为CvrInfo.strCvrName <DataGridTextColumn Header="磁盘阵IP" Width="*" Binding="{Binding strCvrIP}" /> <DataGridTextColumn Header="磁盘阵Port" Width="*" Binding="{Binding strCvrPort}" /> <DataGridTextColumn Header="磁盘阵类型" Width="*" Binding="{Binding strCvrType}" /> <DataGridTextColumn Header="磁盘阵编码" Width="*" Binding="{Binding strCvrCode}" /> </DataGrid.Columns> </DataGrid> </Grid>
以上两种方法都可以进行数据绑定(datagrid, treeview等控件都行), 界面刷新, 第一种方法是在.netfromwork中的4.0的版本支持, 该方法的缺点是当类的属性多了, 如果后期突然改动一下OnPropertyChanged(value)中的value, 界面就不能同步, 而且感觉每个属性都要那么一行代码总感觉怪怪的, 第二种方法是在.netformwork4.5版本支持,避过了方法一种所有的缺点,GG