一直对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="300" Width="540" Background="LightBlue"> <Grid> <Grid.Resources> <Style TargetType="{x:Type Button}"> <Setter Property="Width" Value="60"/> <Setter Property="Height" Value="30"/> <Setter Property="Margin" Value="5"/> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="40*"/> <RowDefinition Height="221*"/> </Grid.RowDefinitions> <WrapPanel Grid.Row="0" Grid.Column="0"> <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="2" Grid.Column="0" 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 = 0; i < 5; 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="1725"+i.ToString() }; Windows.Add(c); } if (this.Windows.Count > 0) { 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 = -1; 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="300" Width="540" 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="160"></Setter> <Setter Property="Height" Value="25" ></Setter> <Setter Property="HorizontalAlignment" Value="Left"></Setter> </Style> <Style TargetType="{x:Type Button}"> <Setter Property="Width" Value="60"></Setter> <Setter Property="Height" Value="25" ></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="1" Grid.Column="1"></Label> <Label Content="评价器地址:" Grid.Row="3" Grid.Column="1"></Label> <Label Content="条屏地址:" Grid.Row="5" Grid.Column="1"></Label> <Label Content="IP:" Grid.Row="7" Grid.Column="1"></Label> <Label Content="设备注册号:" Grid.Row="9" Grid.Column="1"></Label> <Label Content="描述:" Grid.Row="11" Grid.Column="1"></Label> <TextBox x:Name="textboxWin" Grid.Row="1" Grid.Column="2"></TextBox> <TextBox x:Name="textboxEvalutor" Grid.Row="3" Grid.Column="2"></TextBox> <TextBox x:Name="textboxStripeScreen" Grid.Row="5" Grid.Column="2"></TextBox> <TextBox x:Name="textboxIP" Grid.Row="7" Grid.Column="2"></TextBox> <TextBox x:Name="textboxRegNum" Grid.Row="9" Grid.Column="2"></TextBox> <TextBox x:Name="textboxDec" Grid.Row="11" Grid.Column="2"></TextBox> <Button Content="确定" x:Name="btnConfirm" Grid.Row="13" Grid.Column="2" 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)?0:Convert.ToInt32(this.textboxEvalutor.Text.Trim()); } set{this.textboxEvalutor.Text=value.ToString();} } //获取或设置条屏地址 public int StripeScreen { get { return string.IsNullOrWhiteSpace(this.textboxStripeScreen.Text) ? 0 : 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; } } } }
时间: 2024-09-30 21:30:09