一個綜合性的WPF例子
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
本例重點:
資源的使用,型別轉換,將筆刷(這裡是LinearGradientBrush)應用於ListBox控制元件,BitmapEffect, DataTemplate的使用等.
<Page xmlns="
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>
<sys:String>Hello, World!</sys:String>
<sys:Int32>100</sys:Int32>
<sys:Decimal>188</sys:Decimal>
<sys:Int32>68
<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>
有空再作闡述.