使用winform编写商场收银系统
主要是以学习为主,以前用Winform写过简单商场收银系统,现在转换到使用WPF来实现
目录
②使用属性元素(Property Element)进行复杂赋值
第一部分、WPF基础知识
对象属性赋值
XAML是一种声明性语言,XAML编译器会为每个标签创建一个与之对应的对象。创建对象后,需对其属性初始化。因为XAML语言不能编写程序的运行逻辑,所以一份XAML文档中除了使用标签声明对象就是初始化对象的属性。
XAML中为对象属性赋值共两种语法(例子为Rectangle类对象的Fill属性):
①使用字符串进行简单赋值
Attribute1=Value1
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Rectangle x:Name="rectangle" Width="200" Height="120" Fill="Blue"/>
</Grid>
更复杂的字符串实例
<Path Data="M 0,0 L 0,65 L 65,0 Z" Stroke="Transparent" Fill="HotPink"/>
由于使用Attribute1=Value1存在缺点:Value只能是一个字符串。如果一个类只能使用XAML语言进行声明,并允许其Property(面向对象理论范畴,对客观实物进行抽象出来的属性)与XAML标签的Attribute(编程语言文法层面,用于区分两个同类语法元素A和B的特征)互相映射,那就需要为这些Property进行转换。
定义TypeConverter类的派生类,重写TypeConverter的方法
使用TypeConverter类将XAML标签的Attribute与对象的Property进行映射
.cs文件中的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ShopWpf
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//先把Window资源检索出来,窗体自己的方法FindResource,把字典给方法,返回类型为object,要转换为Staff
Staff staff = this.FindResource("staff") as Staff;
if (staff != null)
{
MessageBox.Show("当前员工姓名:"+ staff.Name + "\n" +
"员工号码:" + staff.Telephone + "\n" +
"员工家庭成员:" + staff.Familymember.Name); //staff的Name属性本身就是字符串类型
}
}
}
//NameToStaffTypeConverter类以特性形式附加到Staff类上去
[TypeConverterAttribute(typeof(NameToStaffTypeConverter))]
public class Staff
{
public string Name { get; set; }
public string Telephone { get; set; }
public Staff Familymember { get; set; } //职员的家庭成员也有名字和电话号码
}
public class NameToStaffTypeConverter : TypeConverter //定义TypeConverter的派生类
{ //给出staff中的Familymember一个字符串就可以基于这个字符串就可以声明一个Staff的对象并且把这个字符串赋值给Name属性
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) //重写方法
{
string name = value.ToString();
Staff familymember = new Staff();
familymember.Name = name;
return familymember;
}
}
对应的.xaml文件
<Window x:Class="ShopWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cl="clr-namespace:ControlLibrary;assembly=ControlLibrary"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:ShopWpf"
Title="商场收银系统" Height="600" Width="450" WindowStartupLocation="CenterScreen">
<!--local:因为ShopWpf命名空间包含在当前程序集中,一般将当前程序集包含的命名空间映射为local的名称空间-->
<!--启动在屏幕正中央-->
<Window.Resources>
<!--通过window的资源声明出来,添加资源,资源字典,通过索引检索,属性标签形式,添加字符串,用于标签扩展-->
<!--Staff对象作为Window资源声明出来-->
<local:Staff x:Key="staff" Name="Alice" Telephone="10000000" Familymember="Tom"/>
<!--Staff对象检索存在于命名空间中Key对象-->
<!--通过标签形式声明一个Staff对象-->
</Window.Resources>
<Grid>
<Button Content="登录" HorizontalAlignment="Left" VerticalAlignment="Top" Height="46" Background="Transparent" BorderBrush="Transparent" RenderTransformOrigin="0.5,0.5" Margin="-28,-7,0,0" Width="88" Click="Button_Click">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-45"/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
</Button>
</Grid>
</Window>
②使用属性元素(Property Element)进行复杂赋值
在XAML中,非空标签均具有内容(Content),标签内容指在开始标签和结束标签之间的一些子级标签,每个子级都是父级标签内容的一个元素(Element),简称为父级标签的一个元素,属性标签指的是某个标签的一个元素对应这个标签的一个属性,即以元素的形式来表达一个实例的属性。
<ClassName>
<ClassName.PropertyName>
<!--以对象形式为属性赋值-->
</ClassName.PropertyName>
</ClassName>
以上一个例子改写
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Rectangle x:Name="rectangle" Width="200" Height="120">
<Rectangle.Fill>
<SoildColorBrush="Blue"/>
</Rectangle>
</Grid>
通过使用属性元素来进行赋值,可以设计一个渐变色的Canvas
<Canvas>
<Canvas.Background>
<LinearGradie