序:
基础知识储备
翻一翻,《深入浅出WPF》了解下基本知识也是很重要的。
《深入浅出WPF》 http://pan.baidu.com/s/1i3lOA37 ( 基础中的基础请务必翻一翻,务必!!!)
《wpf编程宝典》 http://pan.baidu.com/s/1sjMgfhj (这本书还没看完,感觉好像挺厉害的,就分享一下。基本看了上一本就能写一些项目了)。
之后我将一些小例子,完整得分享。
一.Navigation 导航栏制作
1,建立一个name 为example的wpf项目
2.放入一下代码即可
<Window x:Class="example.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:example" mc:Ignorable="d" Title="MainWindow" Height="350" Width="1200" WindowStyle="None" ResizeMode="NoResize"> <Window.Resources> <!-- Theme color --> <SolidColorBrush x:Key="FrameHeaderColor" Color="#155abb" /> <SolidColorBrush x:Key="FrameLogoBgColor" Color="#FF0E4EA9" /> <SolidColorBrush x:Key="LeftMenuFontColor" Color="#FF797979" /> <!--textBlock frame header navigation --> <Style x:Key="HeaderTextBlock" TargetType="{x:Type TextBlock}"> <Setter Property="Foreground" Value="#fff" /> <Setter Property="FontSize" Value="16" /> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="Margin" Value="30,15,0,15"/> </Style> <!--header logo style--> <Style x:Key="HeadetLogoImg" TargetType="{x:Type Image}"> <Setter Property="Width" Value="20"/> <Setter Property="Height" Value="20"/> </Style> <!--header navigation--> <Style x:Key="HeaderMenu" TargetType="{x:Type Menu}"> <Setter Property="Background" Value="{StaticResource FrameHeaderColor}"/> <Setter Property="Foreground" Value="#fff"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="Height" Value="50"/> <Setter Property="FontSize" Value="16"/> <Setter Property="Margin" Value="107,0,0,0"/> <Setter Property="Cursor" Value="Hand"></Setter> </Style> <!--MenuItem样式修改--> <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}"> <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <Grid VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3,0,3,3" SnapsToDevicePixels="True" Width="16" VerticalAlignment="Center" RenderOptions.BitmapScalingMode="NearestNeighbor" RecognizesAccessKey="True"/> <Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/> <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" /> <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom"> <Border x:Name="SubMenuBorder" BorderBrush="#FF999999" BorderThickness="1" Background="#FFF" Padding="0"> <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"> <Grid RenderOptions.ClearTypeHint="Enabled"> <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/> </Canvas> <!--<Rectangle Fill="#FFD7D7D7" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/>--> <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/> </Grid> </ScrollViewer> </Border> </Popup> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="#57c52c"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}"> <Setter Property="Height" Value="50"/> <Setter Property="Padding" Value="0,0,25,0"/> <Setter Property="BorderThickness" Value="0,0,0,4"/> <Setter Property="BorderBrush" Value="{DynamicResource FrameHeaderColor}"/> <Setter Property="Template" Value="{StaticResource MenuItemControlTemplate1}"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="BorderBrush" Value="#57c52c"/> <Setter Property="Background" Value="Transparent"></Setter> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid Margin="0,0,0,0"> <Grid.RowDefinitions> <RowDefinition Height="50"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Grid Grid.Row="0" Background="{DynamicResource FrameHeaderColor}" Margin="0,0,0,0" Height="Auto" VerticalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="172"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="175"/> </Grid.ColumnDefinitions> <Grid Grid.Column="0"> <StackPanel Grid.Column="0" Orientation="Horizontal" FlowDirection="LeftToRight" Grid.Row="0"> <Grid Width="50" Height="50" Background="{DynamicResource FrameLogoBgColor}"/> <Grid HorizontalAlignment="Left" VerticalAlignment="Center" Margin="-36,0,0,0" > <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal" VerticalAlignment="Center"> <Image Style="{StaticResource HeadetLogoImg}"/> <TextBlock Style="{StaticResource HeaderTextBlock}"><Run Text="WPF导航栏"/></TextBlock> </StackPanel> /Grid> </StackPanel> </Grid> <Grid Grid.Column="1"> <Menu Style="{StaticResource HeaderMenu}" > <MenuItem Header="菜单一" Click="MenuItem_Click" /> <MenuItem Header="菜单二"/> <MenuItem Header="菜单三"/> <MenuItem Header="菜单四"/> <MenuItem Header="菜单五"/> </Menu> </Grid> <Grid Grid.Column="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0"> <Menu Background="#FF155ABB"> <MenuItem Header="LOKI" Foreground="White" Background="#FF155ABB" BorderThickness="0" BorderBrush="{x:Null}" Padding="0,0,0,0" FontSize="14" > </MenuItem> <MenuItem Padding="0,0,0,0" BorderThickness="0" BorderBrush="{x:Null}" Cursor="Hand" VerticalAlignment="Center" Background="#FF155ABB" > <MenuItem.Icon> <Image Source="Resources\Images\frame_icon_darrow.png" /> </MenuItem.Icon> <MenuItem Header="退出" BorderThickness="0" BorderBrush="{x:Null}" Cursor="Hand"/> </MenuItem> <MenuItem Background="#FF155ABB" Padding="0,0,0,0" BorderThickness="0" BorderBrush="{x:Null}" Cursor="Hand" > <MenuItem.Icon> <Image Source="Resources\Images\frame_icon_setting.png"/> </MenuItem.Icon> </MenuItem> </Menu> </StackPanel> <StackPanel Grid.Column="1" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Right" Background="{StaticResource FrameHeaderColor}"> <Button Width="15" Style="{x:Null}" Cursor="Hand" BorderThickness="0" Height="16" BorderBrush="{StaticResource FrameHeaderColor}" Padding="0" Margin="0,0,10,0"> <Image Source="Resources\Images\frame_icon_min.png"/> </Button> <Button Width="16" Cursor="Hand" Style="{x:Null}" BorderThickness="0" Height="16" Padding="0" Margin="0,0,10,0"> <Image Source="Resources\Images\frame_icon_normal.png"/> </Button> <Button Width="15" Cursor="Hand" Style="{x:Null}" BorderThickness="0" Height="16" Padding="0" Margin="0,0,10,0" Command="{Binding CommandCloseWindow}"> <Image Source="Resources\Images\frame_icon_close.png"/> </Button> </StackPanel> </Grid> </Grid> </Grid> </Window>