[ZT]一个综合性的WPF例子

  http://blog.csdn.net/johnsuna/archive/2008/03/09/2159759.aspx

本例重点:
资源的使用,类型转换,将笔刷(这里是LinearGradientBrush)应用于ListBox控件,BitmapEffect, DataTemplate的使用等.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <x:Array x:Key="array" Type="sys:Object">
      <sys:DateTime>2101-09-22</sys:DateTime>
      <sys:DateTime>2008-1-4 12:05:08</sys:DateTime>
      <sys:String>Hello, World!</sys:String>
      <sys:Int32>100</sys:Int32>
      <sys:Decimal>188</sys:Decimal>
      <sys:Int32>68</sys:Int32>
      <sys:String>WPF Book</sys:String>
    </x:Array>

    <LinearGradientBrush x:Key="borderBrush" StartPoint="0,0" EndPoint="0,1" Opacity="1">
      <LinearGradientBrush.GradientStops>
        <GradientStopCollection>
          <GradientStop Color="Yellow" Offset="0" />
          <GradientStop Color="Red" Offset="1" />
        </GradientStopCollection>
      </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="commonLGB" StartPoint="0,0" EndPoint="0,1">
      <GradientBrush.GradientStops>
        <GradientStopCollection>
          <GradientStop Color="Orange" Offset="0" />
          <GradientStop Color="#FFFF00" Offset="1" />
        </GradientStopCollection>
      </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <DropShadowBitmapEffect x:Key="dropShadow" Color="Black" Direction="315" ShadowDepth="5" Softness="5" Opacity="0.75"/>

    <DataTemplate x:Key="myTaskTemplate">
      <StackPanel Width="160">
        <TextBlock Text="{Binding Path=.}" />
      </StackPanel>
      <DataTemplate.Resources>
        <Style TargetType="TextBlock">
          <Setter Property="FontSize" Value="12"/>
          <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
        <Style TargetType="StackPanel">
          <Setter Property="Background" Value="{StaticResource commonLGB}">
          </Setter>
        </Style>
      </DataTemplate.Resources>

    </DataTemplate>
    <DataTemplate DataType="{x:Type sys:String}">
      <Border BorderBrush="Blue" BorderThickness="1" Background="#EEFFFF">
        <TextBlock Text="{Binding}" Foreground="Brown" FontWeight="Bold" Padding="5"/>
      </Border>
    </DataTemplate>
    <DataTemplate DataType="{x:Type sys:DateTime}">
      <Border BorderBrush="Green" BorderThickness="2" CornerRadius="5">
        <TextBlock Text="{Binding}" Foreground="Green" FontWeight="Bold" Padding="5"/>
      </Border>
    </DataTemplate>
    <DataTemplate DataType="{x:Type sys:Int32}">
      <Border BorderBrush="Purple" BorderThickness="3" CornerRadius="30">
        <TextBlock Text="{Binding}" Foreground="Purple" FontWeight="Bold" Padding="5"/>
      </Border>
    </DataTemplate>
    <DataTemplate DataType="{x:Type sys:Decimal}">
      <Border BorderBrush="Red" BorderThickness="4" CornerRadius="5" Background="Yellow">
        <TextBlock Text="{Binding}" Foreground="DarkOrange" FontWeight="Bold" Padding="30"/>
      </Border>
    </DataTemplate>
  </Page.Resources>

  <Canvas>
    <ListBox ItemsSource="{Binding Source={StaticResource array}, Path=.}" IsSynchronizedWithCurrentItem="true"
ItemTemplate="{StaticResource myTaskTemplate}" BorderThickness="3" BorderBrush="{StaticResource borderBrush}"
BitmapEffect="{StaticResource dropShadow}"
 Canvas.Top="5" Canvas.Left="5">

      <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
          <Setter Property="Background">
            <Setter.Value>
              <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                  <GradientStopCollection>
                    <GradientStop Color="Orange" Offset="0" />
                    <GradientStop Color="#FFFF00" Offset="1" />
                  </GradientStopCollection>
                </GradientBrush.GradientStops>
              </LinearGradientBrush>
            </Setter.Value>
          </Setter>
        </Style>
      </ListBox.Style>
    </ListBox>
    <ContentControl Canvas.Left="200" Content="{Binding Source={StaticResource array}, Path=/}" />
  </Canvas>
</Page>

原文地址:https://www.cnblogs.com/godwar/p/1112141.html