标签:
WPF使用哪几种元素作为顶级元素:
1. Window元素
2. Page元素(与Window元素类似,用于可导航的应用程序)
3. Application元素(定义应用程序资源和启动设置)
PS:在一个WPF应用程序中只能有一个顶级元素
1 <Window x:Class="WpfApplication2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 7 </Grid> 8 </Window>
Class是类
xmlns是XML Namespaces的缩写,中文名称是XML(标准通用标记语言的子集)命名空间
Xmlns[:可选的映射前缀]=“名称空间”
为元素命名:(以Grid元素为例)
例一:
1 <Window x:Class="WpfApplication2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid Name="grid1"> 6 7 </Grid> 8 </Window>
例二:
1 <Window x:Class="WpfApplication2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 <Grid.Name>grid1</Grid.Name> 7 </Grid> 8 </Window>
以上两个例子等效
以上是UI层面的代码
逻辑代码层面调用上面的Grid元素的Name属性为例:
下面代码执行后会把窗体的标题改为Grid元素的名字属性(试了<Grid.Name>grid1</Grid.Name>无效)
UI层代码
1 <Window x:Class="WpfApplication2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 5 <Grid Name="grid1"> 6 </Grid> 7 </Window>
逻辑层代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace WpfApplication2 17 { 18 /// <summary> 19 /// MainWindow.xaml 的交互逻辑 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 public MainWindow() 24 { 25 InitializeComponent(); 26 } 27 28 private void Window_Loaded(object sender, RoutedEventArgs e) 29 { 30 this.Title = this.grid1.Name; 31 } 32 } 33 }
标签:
原文地址:http://www.cnblogs.com/start-from-scratch/p/5484584.html