WPF揭秘--学习笔记
XAML与WPF可以独立分开使用,
XAML定义:
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Content="ok"/>
上面一段代码是xaml格式直接可以再浏览器中打开,一个为“ok”的按钮
等同于
第二个是在属性中设置
接下来两个功能相同
3.1遍历和打印逻辑树和可视树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public MainWindow() 2 { 3 InitializeComponent(); 4 5 Console.WriteLine("LogicalTree Structure:"); 6 EnumLogicalTree(0, this); 7 8 } 9 10 protected override void OnContentRendered(EventArgs e) 11 { 12 base.OnContentRendered(e); 13 14 Console.WriteLine("VisualTree Structure:"); 15 EnumVisualTree(0, this); 16 } 17 18 //遍历视觉树 19 public void EnumVisualTree(int Ident, Visual visualObj) 20 { 21 for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualObj); i++) 22 { 23 //接收特定索引的子元素 24 Visual childVisual = (Visual)VisualTreeHelper.GetChild(visualObj, i); 25 Console.WriteLine(new string(' ', Ident) + childVisual); 26 27 EnumVisualTree(Ident + 1, childVisual); 28 } 29 } 30 31 //遍历逻辑树 32 public void EnumLogicalTree(int Ident, object logObj) 33 { 34 if (!(logObj is DependencyObject))//对象必须派生自DependencyObject对象 35 return; 36 37 foreach (object childLogical in LogicalTreeHelper.GetChildren(logObj as DependencyObject)) 38 { 39 Console.WriteLine(new string(' ', Ident) + childLogical); 40 41 EnumLogicalTree(Ident + 1, childLogical); 42 } 43 }
可以用元素自己的实例方法在两种树中进行操作。例如,Visual类包含了3个
protected的成员(VisualParent、VisualChildrenCount和GetVisualChild)用于验证它的可视父
节点和孩子节点。
4.控件--内容控件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ComboBox> <!-- Item #1 --> <StackPanel Orientation="Horizontal" Margin="5"> <Image Source="CurtainCall.bmp"/> <StackPanel Width="200"> <TextBlock Margin="5,0" FontSize="14" FontWeight="Bold" VerticalAlignment="center">Curtain Call</TextBlock> <TextBlock Margin="5" VerticalAlignment="center" TextWrapping="Wrap"> Whimsical, with a red curtain background that represents a stage. </TextBlock> </StackPanel> </StackPanel> <!-- Item #2 --> <StackPanel Orientation="Horizontal" Margin="5"> <Image Source="Fireworks.bmp"/> <StackPanel Width="200"> <TextBlock Margin="5,0" FontSize="14" FontWeight="Bold" VerticalAlignment="center">Fireworks</TextBlock> <TextBlock Margin="5" VerticalAlignment="center" TextWrapping="Wrap"> Sleek, with a black sky containing fireworks. When you need to celebrate PowerPoint-style, this design is for you! </TextBlock> </StackPanel> </StackPanel> </ComboBox> </Canvas>
http://v.youku.com/v_show/id_XMTkxOTM5NTQw.html