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)。

  

posted on 2016-04-04 10:14  cutepig  阅读(1247)  评论(0编辑  收藏  举报

导航