手把手教你:親手打造Silverlight的Win8 Metro外觀(2) 菜單


 

經過上一篇的分析,現在開始動手!

首先來看一下. 我們的菜單他的表現形式是如何的.

比如說我們現在有一個數據庫叫  lonix_gls_2  有一個表menu (菜單)

id 為主鍵,  fore_id 為上級id  , name 為菜單名 , Code為菜單所對面的xaml文件名

菜單為樹形結構.數據如下,  紅色為 主菜單

T-SQL 代碼
 1 SET ANSI_NULLS ON
 2 GO
 3 SET QUOTED_IDENTIFIER ON
 4 GO
 5 IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[menu]') AND type in (N'U'))
 6 BEGIN
 7 CREATE TABLE [dbo].[menu](
 8     [id] [int] IDENTITY(1,1) NOT NULL,
 9     [name] [nvarchar](10) NULL,
10     [foreid] [int] NULL,
11 PRIMARY KEY CLUSTERED 
12 (
13     [id] ASC
14 )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
15 ) ON [PRIMARY]
16 END


我們用RIA SERVICE 所查出來的代碼結構應該如下

 1 new List<TestModel> 
 2             {
 3                 new TestModel {
 4                     Name = "編碼設置",
 5                     Children = new List<TestModel> { 
 6                         new TestModel { Name = "00101", Code="Test" } ,
 7                         new TestModel { Name = "00102", Code="Test1" } ,
 8                         new TestModel { Name = "00103"  } 
 9                     } 
10                 },
11                 new TestModel {
12                     Name = "系統設置",
13                     Children = new List<TestModel> { 
14                         new TestModel { Name = "00201" } 
15                     }
16                 },
17             };


以上為測試數據..

打開VS  新建三個模版控件!

新建的類默認從  Control  繼承. 我們需要手動將其改為從 ItemsControl繼承..

總共新建 

Desktop(繼承ItemsControl[主菜單容器])  以作用於包含任意項的 DesktopGroup

DesktopGroup(繼承ItemsControl[分組菜單容器])  以作用於包含任意項的 DesktopItem

DesktopItem(繼承ContentControl[子主菜單項])

 

一. Desktop  主菜單容器

1. 使desktop 中的子項 橫向自動布局, 因此我們將他的ItemsPanel 設為StackPanle

2. 使循環綁定子項

3. 容器的布局

代碼如下:

 1     <Style TargetType="local:Desktop">
 2         <Setter Property="ItemsPanel">
 3             <Setter.Value>
 4                 <ItemsPanelTemplate>
 5                     <StackPanel x:Name="DesktopRootPanel" Orientation="Horizontal" Background="{x:Null}">
 6                     </StackPanel>
 7                 </ItemsPanelTemplate>
 8             </Setter.Value>
 9         </Setter>
10         <Setter Property="ItemTemplate">
11             <Setter.Value>
12                 <common:HierarchicalDataTemplate>
13                     <local:DesktopGroup ItemsSource="{Binding Path=Children}" />
14                 </common:HierarchicalDataTemplate>
15             </Setter.Value>
16         </Setter>
17         <Setter Property="Template">
18             <Setter.Value>
19                 <ControlTemplate TargetType="local:Desktop">
20                     <Grid>
21                         <Border Background="White" Opacity="0.1">
22                         </Border>
23                         <Border Margin="50,0,50,0">
24                             <ItemsPresenter />
25                         </Border>
26                     </Grid>
27                 </ControlTemplate>
28             </Setter.Value>
29         </Setter>
30     </Style>

 

local 命名空間為 當前項目

xmlns:local="clr-namespace:當前項目"

 

common 命名空間為

xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"

Desktop 數據綁循環為 DesktopGroup

二. DesktopGroup子菜單容器

同Desktop 基本相同

 

 1   <Style TargetType="local:DesktopGroup">
 2         <Setter Property="ItemsPanel">
 3             <Setter.Value>
 4                 <ItemsPanelTemplate>
 5                     <tlk:WrapPanel  x:Name="DesktopGroupRootPanel" Orientation="Vertical" />
 6                 </ItemsPanelTemplate>
 7             </Setter.Value>
 8         </Setter>
 9         <Setter Property="ItemTemplate">
10             <Setter.Value>
11                 <common:HierarchicalDataTemplate>
12                     <local:DesktopItem />
13                 </common:HierarchicalDataTemplate>
14             </Setter.Value>
15         </Setter>
16         <Setter Property="Template">
17             <Setter.Value>
18                 <ControlTemplate TargetType="local:DesktopGroup">
19                     <Grid Margin="0,0,50,0" Height="Auto">
20                         <Grid.RowDefinitions>
21                             <RowDefinition Height="Auto" />
22                             <RowDefinition Height="*" />
23                         </Grid.RowDefinitions>
24                         <TextBlock Text="{Binding Name}" Grid.Row="0" FontSize="15" FontWeight="Black" />
25                         <Border Grid.Row="1"  Background="{TemplateBinding Background}"
26                             BorderBrush="{TemplateBinding BorderBrush}"
27                             BorderThickness="{TemplateBinding BorderThickness}" >
28                             <Border.Effect>
29                                 <BlurEffect Radius="5" />
30                             </Border.Effect>
31                         </Border>
32                         <ItemsPresenter Margin="2" Grid.Row="1" />
33                     </Grid>
34                 </ControlTemplate>
35             </Setter.Value>
36         </Setter>
37     </Style>

DesktopGroup 數據綁定循環為 DesktopItem

DesktopItem相比這二個之下可能有點繁瑣,因此放在下一篇來制作


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM