wpf学习笔记二 深入学习 xaml
1、XAML 主要用于繪制UI界面,最大的優點是能使UI與運行邏輯分離開來,使得整個程序回到邏輯處理上來。
?? 每一個標簽對應.NET Framework類庫的一個控件類。通過設置標簽的Attribute,不僅可以對標簽所對應的控件 ?? 對象Property進行賦值,還可以聲明名稱空間,指定類名等
2、使用attribute給對象的屬性賦值
?? XAML是一種聲明性語言,XAML編譯器會為每個標簽創建一個與之對應的對象,之后要對他的屬性進行初始化 ?? 才會有意義。所以,每個標簽除了聲明對象就是初始化對象的屬性--即給其屬性賦值。賦值方法有兩種:一種 ?? 是字符串簡單賦值(在XAML中賦值),另外一種是使用屬性元素進行復雜賦值(在.cs里面賦值)。下面的 ? 例子用xaml賦值,
??? 1)前臺代碼:
<Window x:Class="firstApplicaton.Window1" ???
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ???
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ???
Title="Window1" Height="300" Width="300"> ???
<Grid> ???????
<Grid.ColumnDefinitions> ???????????
?? <ColumnDefinition Width="*"/> ???????????
? <ColumnDefinition Width="120"/> ???????
</Grid.ColumnDefinitions> ???????
<Grid.RowDefinitions> ???????????
?? <RowDefinition Height="100"/> ???????????
?? <RowDefinition Height="*"/> ???????
</Grid.RowDefinitions> ??????? ????????
?? <Button x:Name="button1" Content="按鈕1" Width="80"? Height="20" Grid.Column="0"?
???? Grid.Row="0" Background="Blue" /> ????????
?? <Button x:Name="button2" Content="按鈕2" Width="80" Height="20" Grid.Column="1" ??????????
??? ?Grid.Row="1"? Background="Fuchsia"/> ???????
? <Rectangle x:Name="正方形1"? Width="80" Height=" 80" Grid.Column="0" Grid.Row="1" ?????????
???? Fill="Red"/> ???
</Grid> </Window>
??????????????????????????
????? 2)后臺代碼修改button 跟rectangle 的 填充色跟 背景顏色
???????
using System;
using System.Collections.Generic;
using System.Linq; using System.Text;
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 firstApplicaton
{ ??
? /// <summary> ??? /// Window1.xaml 的交互邏輯 ??? /// </summary> ???
public partial class Window1 : Window ??
? { ???????
???? public Window1() ???????
????? { ???????????
???????? InitializeComponent(); ???????????
????????? ? //修改rectangle的填充色 ??????????
?????????? ??? SolidColorBrush scb = new SolidColorBrush(); ??
????? ????????? scb.Color = Colors.Green;
??? ???????????? this.rec.Fill = scb;?
??????????? //修改button的背景色
? ???????????? this.button1.Background = scb;
????????????? this.button2.Background = scb;
??????? } ?
??? }
} ??????????
?????????????????????????????
3、TypeConvert類將XAML標簽的Attribute與對象的Property進行映射
? 1)TypeConvert: 提供一種將值類型轉換為其他類型的統一方式。 TypeConverter 通常支持字符串到對象的
????????????????? 轉換,目的是供設計環境中的屬性編輯器使用或者是為了能夠使用 XAML
?????????????????????????????????? 在xaml 語法中,元素英文就attribute value值都是sring類型的 但是 在相印的property卻不都是
????????????????????????????? ???? string,為了能達到attribute與property的一一對應及相互操作 那就需要用上面的這個
????????????????????????????????? ? typeconvert類進行數據類型的轉換。
????? 下面這個例子 自定義一個類 class1 在里面有兩個屬性,一個是name 另一個是 subclass ,在一下代碼中可以看到subclass是class1類型的屬性,但是在 xaml中屬性卻是string? 現在問題來了? 我們怎么能在。cs文件中把sting轉換成 class1類型呢? 這個時候就用到了 typeConvert? 用他去重寫 convertFrom 方法 來實現。
?? 2)xaml源碼
<Window x:Class="firstApplicaton.Window1"
??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??? xmlns:a="clr-namespace:firstApplicaton"
??? Title="Window1" Height="300" Width="300">
?? ??? <Window.Resources>
??????? <a:class1 x:Key="class1" Name="1" subclass="class2">
??????? </a:class1>
??? </Window.Resources>
??? <Grid>
??????? <Grid.ColumnDefinitions>
??????????? <ColumnDefinition Width="*"/>
??????????? <ColumnDefinition Width="120"/>
??????? </Grid.ColumnDefinitions>
??????? <Grid.RowDefinitions>
??????????? <RowDefinition Height="100"/>
??????????? <RowDefinition Height="*"/>
??????? </Grid.RowDefinitions>
??????? ???????? <Button x:Name="button1" Content="按鈕1" Width="80"? Height="20" Grid.Column="0" Grid.Row="0" Background="Blue" Click="button1_Click" />
???????????? ??? <Button x:Name="button2" Content="按鈕2" Width="80" Height="20" Grid.Column="1" Grid.Row="1"? Background="Fuchsia"/>
????????????? ??<Rectangle x:Name="rec"? Width="80" Height=" 80" Grid.Column="0" Grid.Row="1" Fill="Red"/>
??? </Grid> </Window>
3)cs源碼
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;
?
namespace firstApplicaton
{ ???
/// <summary> ??? /// Window1.xaml 的交互邏輯 ??? /// </summary>
??? public partial class Window1 : Window
??? {
??????? public Window1()
??????? {
??????????? InitializeComponent();
??????????? //修改rectangle的填充色
??????????? SolidColorBrush scb = new SolidColorBrush();
??????????? scb.Color = Colors.Green;
??????????? this.rec.Fill = scb;
??????????? //修改button的背景色
??????????? this.button1.Background = scb;
??????????? this.button2.Background = scb;
?
??????? }
?
??????? private void button1_Click(object sender, RoutedEventArgs e)
??????? {
???????????? class1? c =( class1) this.FindResource("class1");
?
??????????? MessageBox.Show(c.subclass.Name);
?
?????????? ??????? }
??? }
??? // 先托管 數據轉換的類
??? [TypeConverter(typeof(StringToClass1TypeConverter))]
??? //目標類
??? public class? class1
??? {
??????? public string Name { get;set;}
??????? public class1 subclass{get;set;}
??? } ???
//重寫數據類型轉換 在默認的數據類型轉換中會自動轉換
??? public class StringToCass1TypeConverter : TypeConverter ???
{
?
??????? public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
??????? {
?
??????????? if (value is string)
??????????? {
?
?????????????? class1 c = new class1();
?
??????????????? h.Name = value as string;
?
??????????????? return c;
?
??????????? }
?
??????????? return base.ConvertFrom(context, culture, value);
?
??????? }
?
??? }
}
轉載于:https://www.cnblogs.com/happygod/archive/2013/01/29/wpf.html
總結
以上是生活随笔為你收集整理的wpf学习笔记二 深入学习 xaml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php常见数据类型转换与判断
- 下一篇: Servlet JSP系列文章总结