WPF开发:
xaml 语言类似于xml,主要是界面和逻辑分离。
两个属性的问题:
attribute/property
第一个属性attribute 和c#类的数据字段对应
第二个属性property 和c#的属性对应 get set
对于c# wpf,property可以绑定数据,而attribute不可以。
xaml有命名空间:
xmlns:是在声明名称控件 xml namespace的缩写
比如:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
http://schemas.microsoft.com/winfx/2006/xaml/presentation
代表了system.window 等很多的命名空间。
这是一个简单的xmal,window对象包含grid对象,
<Window x:Class="WpfApp1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="我的窗体" Height="450" Width="800">
<Grid>
</Grid>
</Window>
x:Class 这个属性的作用是,这个xaml 被解析成c#类之后,这个类的类名是什么。
xaml如何做到界面和逻辑分离:
就是xaml会被解析成c#类和 cs文件进行合并,因为cs文件是partial类。
理论上xaml可以被翻译成c#的语言,但是xaml是xml文本,因此属性的值只能是字符串。
在实际中,一定有复杂的对象进行赋值,对应到xaml就需要特别处理。
举例:
public class STHTypeConvert : TypeConverter
{
//转换器
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
//
if(value is string)
{
Human human = new Human();
human.Name = value as string;
return human;
}
return base.ConvertFrom(context, culture, value);
}
}
[TypeConverterAttribute(typeof(STHTypeConvert))]
class Human
{
public string Name { get; set; }
public Human Child { get; set; }
}
//1.创建资源
<Application.Resources>
<local:Human x:Key="human" Child="BNF"></local:Human>
</Application.Resources>
//
Human human = this.FindResource("human") as Human;
MessageBox.Show(human.Child.Name);
通过重写转换器,从字符串到对象得到了正确的转换。
//1复杂对象属性赋值
<Rectangle x:Name="rec" Width="100" Height="100">
<Rectangle.Fill>
<SolidColorBrush Color="Blue"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
//2简答属性赋值
<Rectangle x:Name="rec" Width="100" Height="100" Fill="Blue">
</Rectangle>
完全一样的功能,但是第一中在属性比较复杂的时候,更加有用。
x:命名默认空间 x是xmal的简称。
x:空间有很多属性:
x:Class 类
x:name 对象的引用名称
x:Key 资源检索 把使用的资源放到资源字典中,使用key进行检索。
wpf控件:数据驱动UI
布局:Grid/stackPanel/
一个布局的例子:留言板
<Grid ShowGridLines="False" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" MinWidth="120"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition Width="4"></ColumnDefinition>
<ColumnDefinition Width="80"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="4"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="4"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="请选择部门留言" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center"></TextBlock>
<ComboBox Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="4"></ComboBox>
<TextBox Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="5" BorderBrush="Black"></TextBox>
<Button Content="提交" Grid.Column="2" Grid.Row="4" ></Button>
<Button Content="清除" Grid.Column="4" Grid.Row="4" ></Button>
</Grid>
stackPanel的特点:搭积木。横着排列 竖着排列。
Dockpanel:切分空间,就像传播靠岸一样。
<DockPanel>
<TextBox DockPanel.Dock="Top" Height="30" BorderBrush="Black"></TextBox>
<TextBox DockPanel.Dock="Left" Width="150" BorderBrush="Black"></TextBox>
<TextBox BorderBrush="Black"></TextBox>
</DockPanel>
实现可拖拽的分隔栏:使用grid/gridspliter
<Grid ShowGridLines="False" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Row="1" BorderBrush="Black"></TextBox>
<GridSplitter Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="5" Background="Gray" ShowsPreview="True"></GridSplitter>
<TextBox Grid.Row="1" Grid.Column="2" BorderBrush="Black"></TextBox>
</Grid>
数据绑定:
数据绑定是UI和业务逻辑可以连接起来。
绑定的关键是:目标对象 ,数据对象,目标对象的属性,数据对象的属性(path)
数据绑定的例子:
//数据对象
class Student:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name { get { return name; } set { name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
} }
}
//执行绑定
stu = new Student();
Binding binding = new Binding();
binding.Source = stu;
binding.Path = new PropertyPath("Name");
BindingOperations.SetBinding(this.txt, TextBox.TextProperty, binding);
//修改对象属性的属性
private void Button_Click(object sender, RoutedEventArgs e)
{
//
stu.Name += "name";
}
//目前元素的改变会呈现在界面
绑定的方式 单向的 双向的
绑定的路径 数据对象的属性
如果数据对象是简单数据 path=.
DataContext 这是属性是所有wpf空间都具有的属性。
1.列表控件使用集合数据
class Student
{
public string Name { get; set; }
public string Remark { get; set; }
}
<StackPanel>
<TextBlock Text="选择的编号"></TextBlock>
<TextBox x:Name="txt"></TextBox>
<TextBlock Text="列表"></TextBlock>
<ListBox x:Name="stus" Height="100"></ListBox>
</StackPanel>
List<Student> students = new List<Student>() {
new Student(){ Name="stu1",Remark="9999"},
new Student(){ Name="stu2",Remark="99991"},
new Student(){ Name="stu3",Remark="99992"},
new Student(){ Name="stu4",Remark="99993"},
};
stus.ItemsSource = students;
stus.DisplayMemberPath = "Name";
Binding bind = new Binding("SelectedItem.Remark") { Source=stus};
txt.SetBinding(TextBox.TextProperty, bind);
//
文本框绑定了选择的项目的remark属性。
<ListBox x:Name="stus" Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Width="100" Background="AliceBlue"></TextBlock>
<TextBlock Text="{Binding Remark}" Width="100"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
//数据模板 为每一个item设定模板。 默认只显示一个字段,这里可以显示多个字段。
数据模板:
DataTemplate
//
表格控件的xmal:
<DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Column1" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Column2" Binding="{Binding LastName}" />
<DataGridHyperlinkColumn Header="Column3" Binding="{Binding Email}" />
<DataGridCheckBoxColumn Header="Column4" Binding="{Binding IsMember}" />
</DataGrid.Columns>
</DataGrid>
https://blog.csdn.net/colingg/article/details/80614257