初步理解,数据绑定就是程序中变量改变时候,自动更新UI中的该变量的值。
1.构建数据访问组件,如(仓库,SerialPort等)。
2.构建数据对象, 如(仓库存储的商品,SerialPort统计收发次数的变量等)。该对象将在UI中显示。
3.显示绑定对象。
以wpf中使用SerialPort为例,组件就是SerialPort。然后我们创建一个SerialPortModel:SerialPort对象,在这对象中我们可以在界面显示它的一些变量,比如端口,波特率,收发的bit总数等。
public class SerialPortModel : SerialPort, INotifyPropertyChanged
{
//public SerialPort Port { get; set; }
//DispatcherTimer
private string[] portnames; //要绑定显示的变量
public string[] PortNames
{
get { return portnames; }
set
{
portnames = value;
RaisePropertyChanged("PortNames");
}
}
...
}
相应的在xmal中像是绑定对象
<ComboBox ItemsSource="{Binding Port.PortNames}" x:Name="cmbPortName" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center"></ComboBox>