wpf揭秘

2.4属性元素

以下c#和xaml是一致的

Rectangle r = new Rectangle();
r.Width = 40;
r.Height = 40;
r.Fill = Brushes.Black;
button.Content = r;

 

<Button x:Name="button" HorizontalAlignment="Left" Height="120" Margin="81,61,0,0" VerticalAlignment="Top" Width="346" Click="button_Click">
<Button.Content>
<Rectangle Height="40" Width="40" Fill="red">
</Rectangle>
</Button.Content>
</Button>

 2.6属性扩展

以下xaml

<Button x:Name="button" Click="button_Click" Background="{x:Null}" Height="{x:Static SystemParameters.IconHeight}" Content="{Binding Path=Height, RelativeSource={RelativeSource Self}}">

和c#等价

button.Background = null;
button.Height = SystemParameters.IconHeight;
System.Windows.Data.Binding binding = new Binding();
binding.Path = new PropertyPath("Height");
binding.RelativeSource = RelativeSource.Self;
button.SetBinding(Button.ContentProperty, binding);

 

content binding含义:显示在button上的字符串

 

2.7.2 集合项

<ListBox x:Name="listBox" >
<ListBox.Items>
<ListBoxItem Content="Item 1"></ListBoxItem>
<ListBoxItem Content="Item 2"></ListBoxItem>
</ListBox.Items>
</ListBox>

 

等价于

System.Windows.Controls.ListBoxItem li1 = new System.Windows.Controls.ListBoxItem();
li1.Content = "Item 1";
listBox.Items.Add(li1);

System.Windows.Controls.ListBoxItem li2 = new System.Windows.Controls.ListBoxItem();
li2.Content = "Item 2";
listBox.Items.Add(li2);

 3.2逻辑树与可视树

代码

   public MainWindow()
        {
            InitializeComponent();
            printLogicalTree(0, this);
          
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A");
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);
            printVisualTree(0, this);
        }
        void printLogicalTree(int depth, object obj)
        {
            Debug.WriteLine(new string(' ', depth) + obj);
            if (!(obj is DependencyObject)) return;
            foreach(object child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
            {
                printLogicalTree(depth + 1, child);
            }
        }

        void printVisualTree(int depth, DependencyObject obj)
        {
            Debug.WriteLine(new string(' ', depth) + obj);
            if (!(obj is DependencyObject)) return;
            for(int i=0; i< VisualTreeHelper.GetChildrenCount(obj);i++)
            {
                printVisualTree(depth + 1, VisualTreeHelper.GetChild(obj,i));
            }
        }

  

输出

WpfApplication1.MainWindow
 System.Windows.Controls.StackPanel
  System.Windows.Controls.Label: eeeeeee
   eeeeeee
  System.Windows.Controls.Label: Label
   Label
  System.Windows.Controls.Label: Label
   Label
  System.Windows.Controls.ListBox Items.Count:0
  System.Windows.Controls.StackPanel
   System.Windows.Controls.Button: Button
    Button
   System.Windows.Controls.Button: Button
    Button
  System.Windows.Controls.Primitives.StatusBar Items.Count:1
   xxx
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXmlLinq\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXmlLinq.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXml\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXml.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationCore.resources\v4.0_4.0.0.0_zh-Hans_31bf3856ad364e35\PresentationCore.resources.dll”。模块已生成,不包含符号。
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\Users\cutepig\AppData\Local\Temp\VisualStudio.XamlDiagnostics.8476\WpfXamlDiagnosticsTap.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\windows\assembly\GAC\Microsoft.VisualStudio.OLE.Interop\7.1.40304.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.OLE.Interop.dll”。模块已生成,不包含符号。
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll”。无法查找或打开 PDB 文件。
“WpfApplication1.vshost.exe”(CLR v4.0.30319: WpfApplication1.vshost.exe): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationProvider\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationProvider.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
WpfApplication1.MainWindow
 System.Windows.Controls.Border
  System.Windows.Documents.AdornerDecorator
   System.Windows.Controls.ContentPresenter
    System.Windows.Controls.StackPanel
     System.Windows.Controls.Label: eeeeeee
      System.Windows.Controls.Border
       System.Windows.Controls.ContentPresenter
        System.Windows.Controls.TextBlock
     System.Windows.Controls.Label: Label
      System.Windows.Controls.Border
       System.Windows.Controls.ContentPresenter
        System.Windows.Controls.TextBlock
     System.Windows.Controls.Label: Label
      System.Windows.Controls.Border
       System.Windows.Controls.ContentPresenter
        System.Windows.Controls.TextBlock
     System.Windows.Controls.ListBox Items.Count:0
      System.Windows.Controls.Border
       System.Windows.Controls.ScrollViewer
        System.Windows.Controls.Grid
         System.Windows.Shapes.Rectangle
         System.Windows.Controls.ScrollContentPresenter
          System.Windows.Controls.ItemsPresenter
           System.Windows.Controls.VirtualizingStackPanel
          System.Windows.Documents.AdornerLayer
         System.Windows.Controls.Primitives.ScrollBar 最小值:0 最大值:0 值:0
         System.Windows.Controls.Primitives.ScrollBar 最小值:0 最大值:0 值:0
     System.Windows.Controls.StackPanel
      System.Windows.Controls.Button: Button
       Microsoft.Windows.Themes.ButtonChrome
        System.Windows.Controls.ContentPresenter
         System.Windows.Controls.TextBlock
      System.Windows.Controls.Button: Button
       Microsoft.Windows.Themes.ButtonChrome
        System.Windows.Controls.ContentPresenter
         System.Windows.Controls.TextBlock
     System.Windows.Controls.Primitives.StatusBar Items.Count:1
      System.Windows.Controls.Border
       System.Windows.Controls.ItemsPresenter
        System.Windows.Controls.DockPanel
         System.Windows.Controls.Primitives.StatusBarItem: xxx
          System.Windows.Controls.Border
           System.Windows.Controls.ContentPresenter
            System.Windows.Controls.TextBlock
   System.Windows.Documents.AdornerLayer
线程 0xb10 已退出,返回值为 0 (0x0)。

  

Windows Presentation Foundation (WPF) 是.NET Framework 3.0 的关键组件,是支持下一代视窗应用程序表现层编程的平台,也是微软新发布的Vista操作系统的三大核心开发库之一,主要负责图形显示。本书是针对那些对用户界面开发感兴趣的软件开发人员编写的,易于理解,适合那些.NET的新手,并有助于理解像Microsoft Expression Blend这样产品的精髓。   本书适合各层次Web开发人员阅读。 第一部分 背景 第1章 为什么创造WPF    1.1 回顾过去    1.2 步入WPF    1.3 作为.NET Framework的组成部分     1.3.1 为托管代码而设计     1.3.2 强调声明式描述    1.4 小结   第2章 XAML揭秘    2.1 XAML定义    2.2 元素和特性    2.3 命名空间    2.4 属性元素    2.5 类型转换器    2.6 标记扩展    2.7 对象元素的子元素     2.7.1 内容属性     2.7.2 集合项     2.7.3 更多类型转换    2.8 编译:将XAML与过程式代码混合使用     2.8.1 在运行时加载和解析XAML     2.8.2 编译XAML     2.8.3 XAML关键字    2.9 小结     2.9.1 抱怨1:XML太过冗长不便于输入     2.9.2 抱怨2:基于XML的系统性能差   第3章 WPF的重要新概念    3.1 逻辑树与可视树    3.2 依赖属性     3.2.1 依赖属性的实现     3.2.2 变更通知     3.2.3 属性值继承     3.2.4 对多个提供程序的支持     3.2.5 附加属性    3.3 路由事件     3.3.1 路由事件的实现     3.3.2 路由策略和事件处理程序     3.3.3 路由事件实践     3.3.4 附加事件    3.4 命令     3.4.1 内建命令     3.4.2 使用输入手势执行命令     3.4.3 带有内建命令绑定的控件    3.5 漫游类层次    3.6 小结  第二部分 构建WPF应用程序  第4章 WPF控件   第5章 尺寸缩放、定位与变换元素   第6章 使用面板做布局   第7章 构建并部署应用程序  第三部分 为专业开发人员设计的功能  第8章 资源   第9章 数据绑定   第10章 样式、模板、皮肤和主题 233 第四部分 通过富媒体使程序领先于时代  第11章 2D图形   第12章 3D图形   第13章 动画   第14章 音频、视频、语音和文档  第五部分 高级主题  第15章 与Win32、Windows Form以及ActiveX之间的互用性   第16章 用户控件和自定义控件   第17章 使用自定义面板做布局  第六部分 附录 附录 有用的工具 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值