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; } }
}
}
随机推荐
- HTML笔记(一)
HTML注释格式如下: <!-- 这里是注释 --> HTML中的标题(heading)通过h1~h6来定义. 文本格式化标签: 标签 描述 <b> 定义粗体文本. <b ...
- MongoDB开发学习
如果你从来没有接触MongoDB或对MongoDB有一点了解,如果你是C#开发人员,那么你不妨花几分钟看看本文.本文将一步一步带您轻松入门. 阅读目录 一:简介 二:特点 三:下载安装和开启服务器 四 ...
- 使jQuqer更高效的方法
讨论 jQuery 和 javascript 性能的文章并不罕见.然而,本文我计划总结一些速度方面的技巧和我本人的一些建议,来提升你的 jQuery 和 javascript 代码.好的代码会带来速度 ...
- 操作符 Thinking in Java 第三章
3.1 更简单的打印语句 3.2 使用Java操作符 3.3 优先级 *int类型+String类型 直接转换为String类型 3.4 赋值 1. *引用=引用 两个引用指向同一个对象,所以操作 ...
- Forbidden You don't have permission to access / on this server. You don't have permission to access /phpmyadmin/ on this server. 解决办法
Forbidden You don't have permission to access / on this server. 解决办法 打开 httpd.conf 文件, 将 # onli ...
- HTML字符实体
常用实体符号:
- sqlplus入门基础语句
关于Oracle 首先Oracle一个数据库由若干个表空间组成,每个表空间由若干个数据文件(或设备)组成,每个数据文件由若干个盘区组成,每个盘区由若干个block组成.这是Oracle的物理结构. 逻 ...
- python一个注意的地方
https://www.zhihu.com/question/25874136 class test: l=[] def init(self): self.l=['1','2','7'] a1=tes ...
- jQuery的选择器小总结
这一节详细的总结jQuery选择器. 一.基础选择器 $('#info'); // 选择id为info的元素,id为document中是唯一的,因此可以通过该选择器获取唯一的指定元素 $('.in ...
- (五)Super VLAN