一個綜合性的WPF例子

來源:互聯網
上載者:User

本例重點:
資源的使用,類型轉換,將筆刷(這裡是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> 

有空再作闡述.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.