Volunteer .NET Evangelist

A well oiled machine can’t run efficiently, if you grease it with water.
   :: 首页 ::  :: 联系 :: 订阅 订阅 :: 管理

2006年8月28日

1. Declare array object in xaml:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys
="clr-namespace:System;assembly=mscorlib">

    
<Grid>
        
<ListBox Width="200" Height="200" BorderBrush="Navy" BorderThickness="1">
            
<ListBox.ItemsSource>
                
<x:Array Type="{x:Type sys:String}">
                    
<sys:String>Paviel Dedved</sys:String>
                    
<sys:String>Andriy Shevchenko</sys:String>
                    
<sys:String>Paolo Maldini</sys:String>
                    
<sys:String>Robert Baggio</sys:String>
                    
<sys:String>Alessandro Del Piero</sys:String>
                
</x:Array>
            
</ListBox.ItemsSource>
        
</ListBox>
    
</Grid>
</Page>

2.
Declare inline code in Xaml:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
           x:Class
="MyPage">
  
<x:Code>
    
<![CDATA[
      private void ButtonClickEventHandler(Object sender, RoutedEventArgs e)
      {
          MessageBox.Show("Hello Xaml!");
      }
    
]]>
  
</x:Code>
  
<Grid>
    
<Button Width="200" Height="60" Content="Click Me" Click="ButtonClickEventHandler"/>
  
</Grid>
</Page>

    Note that, when specifying inline code in xaml, you also need to specify the x:Class and the Xaml should be compiled before getting execution, which means that XamlReader.Load() method cannot intepret the xaml which contains inline code.

posted @ 2006-10-03 15:18 Sheva 阅读(1196) | 评论 (2)编辑

    Just paste the following xaml into the XamlPad shipped with Microsoft Windows SDK or the XamlPad I create myself, you will get some unexpected surprise, really cool indeed:

<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
         Margin
="10"
         OpacityMask
="#99000000">
    <
Button/>
</
Viewbox>

    This is what I get in my own XamlPad:



posted @ 2006-09-26 20:54 Sheva 阅读(536) | 评论 (0)编辑

     摘要: Some one pointed out on MSDN WPF forum post that FindName breaks on DataTemplate, when we want to reference the visual elements inside the data template, we cannot use FindName, then what we can use? ... 阅读全文

posted @ 2006-09-23 23:05 Sheva 阅读(2059) | 评论 (2)编辑

    When we want to declaratively use our custom controls or reference the types we defined in XAML, we should declare the XML namespace to map to the CLR namespace in which the controls and types are defined as the following XAML snippt demonstrates:

<Page x:Class="Test.MainWindow"
         xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:cc
="clr-namespace:Sheva.Windows.Controls;assembly=AvalonLib"
/>

    Recently, I am heavily engaged with writing a resuable WPF control library, and quite often I need to create some pages to test those controls I create, so ever now and then I have to use the cumbersome syntax like above to declare the XML namespace, personally I don't like the syntax, and I thought Microsoft WPF team should come up with a more elegant way of doing it, and in actuality, they did introduce an alternative to do it since the CTP releases. there is a custom attribute called XmlnsDefinitionAttribute in the System.Windows.Markup namespace which allows you to apply to the assembly which contains the types and controls the XAML code would reference.the built-in WPF assemblies such as WindowsBase.dll, PresentationCore.dll, and PresentationFramework.dll all have this attribute applied, and all have http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace mapped with the CLR namespaces introduced by them. and of cause you can use this attribute in your own project, just open the AssemblyInfo.cs file, and append the following directive:

[assembly: XmlnsDefinition("http://schemas.sheva.com/wpf/""Sheva.Windows.Controls")]

    Then you can declare the namespace alias in XAML this way:

<Page x:Class="Test.MainWindow"
         xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:cc
="http://schemas.sheva.com/wpf/"
/>
   
    Wow, this is a bit like the built-in xmlns declaration indeed, really really cool indeed:)

posted @ 2006-08-28 23:37 Sheva 阅读(1772) | 评论 (0)编辑