C#get,set
一直对get,set的理解只在文字上:get 属性访问器用于返回属性值,而 set 访问器用于分配新值.其实这样理解是有点狭隘的,尤其是对set。set应该可以理解为为成员分配新值时的处理,比如一个类成员Name,对Name赋值时可以同时处理与Name业务上有关的操作。下面一个工作用到的例子(wpf程序):选中一条记录,点击"修改"打开对应页面,代码界面如下:
先上界面图:第一个界面显示详细,第二个界面是点击增加按钮或选中一条数据点击修改按钮时弹出界面

第二个界面(弹出界面)

具体代码实现如下:
//第一个界面xaml页面:
<Window x:Class="WPF_Study.WindowSet"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowSet" Height="" Width="" Background="LightBlue">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Width" Value=""/>
<Setter Property="Height" Value=""/>
<Setter Property="Margin" Value=""/>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="40*"/>
<RowDefinition Height="221*"/>
</Grid.RowDefinitions>
<WrapPanel Grid.Row="" Grid.Column="">
<Button Content="查询" x:Name="btnSearch"></Button>
<Button Content="新增" x:Name="btnAdd"></Button>
<Button Content="修改" x:Name="btnAmend"></Button>
<Button Content="删除" x:Name="btnDelete"></Button>
</WrapPanel>
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Grid.Row="" Grid.Column="" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="窗口编号" Binding="{Binding Win}"/>
<DataGridTextColumn Header="评价器地址" Binding="{Binding Evalutor}"/>
<DataGridTextColumn Header="条屏地址" Binding="{Binding StripeScreen}"/>
<DataGridTextColumn Header="IP" Binding="{Binding IP}"/>
<DataGridTextColumn Header="注册设备号" Binding="{Binding RegNum}"/>
<DataGridTextColumn Header="描述" Binding="{Binding Description}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
//对应的后台代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using WPF_Study.Entity; namespace WPF_Study
{
/// <summary>
/// Interaction logic for WindowSet.xaml
/// </summary>
public partial class WindowSet : Window
{
internal ObservableCollection<QueueClient.Counter> Windows { get; set; }
//修改的行号标识
private int flag; public WindowSet()
{
InitializeComponent();
Windows = new ObservableCollection<QueueClient.Counter>();
this.btnAdd.Click += Button_Click;
this.btnAmend.Click += Button_Click;
this.btnDelete.Click += Button_Click;
this.btnSearch.Click += Button_Click;
//测试手动造数据
for (int i = ; i < ; i++)
{
QueueClient.Counter c = new QueueClient.Counter()
{
Win=i.ToString(),
Description = "Description"+i.ToString(),
Evalutor=i,
StripeScreen=i,
IP="172.100.12.0"+i.ToString(),
RegNum=""+i.ToString()
};
Windows.Add(c);
}
if (this.Windows.Count > )
{
this.dataGrid.ItemsSource = Windows;
} } void Button_Click(object sender, RoutedEventArgs e)
{
Button button = e.OriginalSource as Button;
if(button.Equals(this.btnAdd))
{
WindowUpdate win = new WindowUpdate();
flag = -;
win.Closed += win_Closed;
win.ShowDialog();
}
if(button.Equals(this.btnDelete))
{
QueueClient.Counter c=this.dataGrid.SelectedItem as QueueClient.Counter;
this.Windows.Remove(c);
}
if(button.Equals(this.btnAmend))
{
QueueClient.Counter c = this.dataGrid.SelectedItem as QueueClient.Counter;
flag = Windows.IndexOf(c);
WindowUpdate win = new WindowUpdate(c.Win);
win.Evaluator = c.Evalutor;
win.StripeScreen = c.StripeScreen;
win.IP = c.IP;
win.RegNum = c.RegNum;
win.Description = c.Description;
win.Closed += win_Closed;
win.ShowDialog();
}
} private void win_Closed(object sender, EventArgs e)
{ } }
}
//增加修改界面(共用同一个界面程序)
xaml界面代码:
<Window x:Class="WPF_Study.WindowUpdate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowUpdate" Height="" Width="" Background="LightBlue">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="HorizontalAlignment" Value="Right"></Setter>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Width" Value=""></Setter>
<Setter Property="Height" Value="" ></Setter>
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Width" Value=""></Setter>
<Setter Property="Height" Value="" ></Setter>
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" />
<ColumnDefinition Width="105*" />
<ColumnDefinition Width="246*" />
<ColumnDefinition Width="100*" />
</Grid.ColumnDefinitions>
<Label Content="窗体编号:" Grid.Row="" Grid.Column=""></Label>
<Label Content="评价器地址:" Grid.Row="" Grid.Column=""></Label>
<Label Content="条屏地址:" Grid.Row="" Grid.Column=""></Label>
<Label Content="IP:" Grid.Row="" Grid.Column=""></Label>
<Label Content="设备注册号:" Grid.Row="" Grid.Column=""></Label>
<Label Content="描述:" Grid.Row="" Grid.Column=""></Label>
<TextBox x:Name="textboxWin" Grid.Row="" Grid.Column=""></TextBox>
<TextBox x:Name="textboxEvalutor" Grid.Row="" Grid.Column=""></TextBox>
<TextBox x:Name="textboxStripeScreen" Grid.Row="" Grid.Column=""></TextBox>
<TextBox x:Name="textboxIP" Grid.Row="" Grid.Column=""></TextBox>
<TextBox x:Name="textboxRegNum" Grid.Row="" Grid.Column=""></TextBox>
<TextBox x:Name="textboxDec" Grid.Row="" Grid.Column=""></TextBox>
<Button Content="确定" x:Name="btnConfirm" Grid.Row="" Grid.Column="" Click="btnConfirm_Click"></Button>
</Grid>
</Window>
//后台代码(此界面成员的get,set用法)
using System;
using System.Windows;
using System.Windows.Controls;
namespace WPF_Study
{
/// <summary>
/// Interaction logic for WindowUpdate.xaml
/// </summary>
public partial class WindowUpdate : Window
{
//获取窗口编号
public string Win
{
get { return this.textboxWin.Text; }
private set { this.textboxWin.Text = value; }
}
//获取或设置评价器地址
public int Evaluator
{
get { return string.IsNullOrWhiteSpace(this.textboxEvalutor.Text)?:Convert.ToInt32(this.textboxEvalutor.Text.Trim()); }
set{this.textboxEvalutor.Text=value.ToString();}
}
//获取或设置条屏地址
public int StripeScreen
{
get { return string.IsNullOrWhiteSpace(this.textboxStripeScreen.Text) ? : Convert.ToInt32(this.textboxStripeScreen.Text.Trim()); }
set { this.textboxStripeScreen.Text = value.ToString(); }
}
//获取或设置IP地址
public string IP
{
get { return this.textboxIP.Text; }
set { this.textboxIP.Text = value; }
}
//获取或设置注册设备号
public string RegNum
{
get { return this.textboxRegNum.Text; }
set { this.textboxRegNum.Text = value; }
}
//获取或设置注册设备号
public string Description
{
get { return this.textboxDec.Text; }
set { this.textboxDec.Text = value; }
}
//新增窗口构造函数
public WindowUpdate()
{
InitializeComponent();
} //修改窗口构造函数
public WindowUpdate(string strWin):this()
{
this.Win = strWin;
} private void btnConfirm_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
}
}
//实体
using System;
using System.Xml;
using System.Xml.Serialization; namespace WPF_Study.Entity
{
internal class QueueClient
{ public class Counter
{
[XmlAttribute("Window")]
public string Win { get; set; }
public string Description { get; set; }
[XmlIgnore]
public int Evalutor { get; set; }
[XmlIgnore]
public int StripeScreen { get; set; }
[XmlIgnore]
public string IP { get; set; }
[XmlIgnore]
public string RegNum { get; set; } }
}
}
随机推荐
- Intel微处理器学习笔记(五) 中断
▼ 中断是一个由硬件激发的过程,它中断当前正在执行的任何程序. ▼ 在Intel系列微处理器中,包括INTR和NMI(Non Maskable Interrupt)两个申请中断的引脚和一个响应INTR ...
- iOS - OC 异常处理
1.@try 语句 @try { // Code that can potentially throw an exception 可能会抛出异常的代码块 statement . . . } @catc ...
- iOS - Swift Enum 枚举
1.Enum 的创建 1.1 标准定义 枚举的定义 enum CompassPoint { case North case South case East case West } enum Plane ...
- JS 拼装代码的HTML onClick方法传递字符串
有时会在JS中拼装HTML代码,这时在HTML中出现的onClick()方法中: 1.出现传递Num型的数据,直接拼装进去即可: 2.可能会出现传递字符串的情况,处理方法比较特殊,如下: a:直接字符 ...
- iOS添加Prefix Header
1. 添加Prefix Header 注: Xcode 6苹果默认去掉prefix Header, 用以提高原文件的复用性, 便于迁移. 并且可以一定程度上减少Build Time. 解决办法: (1 ...
- IO端口和IO内存的区别及分别使用的函数接口
每个外设都是通过读写其寄存器来控制的.外设寄存器也称为I/O端口,通常包括:控制寄存器.状态寄存器和数据寄存器三大类.根据访问外设寄存器的不同方式,可以把CPU分成两大类.一类CPU(如M68K,Po ...
- (三)uboot源码分析
一.九鼎官方uboot和三星原版uboot对比(1)以九鼎官方的uboot为蓝本来学习的,以三星官方的这份为对照.(2)不同版本的uboot或者同一版本不同人移植的uboot,可能目录结构和文件内容都 ...
- js boolean 判断
在写项目的时候出现了一个这样的问题,虽然问题解决了,但是还是有点疑问. 在数据库中设计的表的一个字段为是否审核(is_vaild) 类型 tinyint(1) 对应的在 java中就是布尔类型(boo ...
- Akka学习博客
http://www.iteblog.com/archives/1157 以示例介绍了actor模型.
- 转!!各种数据库的jdbc驱动下载及连接方式
各种数据库驱动 数据库名称 下载地址 说明 Mysql http://www.mysql.com/products/connector/j/ Shipped. But need to download ...