PS需要補充的一個問題 下面圖片中出現的跟目錄1 載入了2次 是之前上傳的時候忘記LeftMenu.xaml裡面的這句代碼刪除了
<uc:TabDetail TRootStyle="{Binding RootStyle,ElementName=LeftMenuUC}" TTabDetailText="{Binding LeftMenuText,ElementName=LeftMenuUC}" TRootName="{Binding RootName,ElementName=LeftMenuUC}" TIResource="{Binding IResource,ElementName=LeftMenuUC}" TItemActualHeight="{Binding
ItemActualHeight,ElementName=LeftMenuUC}"></uc:TabDetail>
刪除這句代碼 就會正常顯示 目錄 1 2 3 4
DEMO下載
http://download.csdn.net/detail/qq873113580/4934434
控制項的XAML代碼
<UserControl x:Class="ERPSilverlightDemo.TabDetail" 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" mc:Ignorable="d" DataContext="{Binding RelativeSource={RelativeSource Self}}" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.Resources> <!--上下的動畫--> <Storyboard x:Name="TListBoxIn"> <DoubleAnimation Storyboard.TargetName="TListBox" Storyboard.TargetProperty="Height" Duration="00:00:00.50" To="0"/> </Storyboard> <Storyboard x:Name="TListBoxOut"> <DoubleAnimation Storyboard.TargetName="TListBox" Storyboard.TargetProperty="Height" Duration="00:00:00.50" To="{Binding TItemActualHeight}"/> </Storyboard> </UserControl.Resources> <Grid x:Name="LayoutRoot" Width="200"> <StackPanel> <!--根目錄--> <Grid Style="{Binding TRootStyle}" Tag="0" MouseLeftButtonDown="Grid_MouseLeftButtonDown"> <TextBlock x:Name="root" Text="{Binding TRootName}" Style="{Binding TTabDetailText}"/> </Grid> <!--左邊顯示部分的頭條--> <ListBox x:Name="TListBox" Height="0" ItemsSource="{Binding TIResource}" Background="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" BorderBrush="Transparent"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" Style="{StaticResource LeftMenuTextDetail}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid></UserControl>
後台cs代碼
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using ERPSilverlightDemo.Entities;using System.Collections;namespace ERPSilverlightDemo{ public partial class TabDetail : UserControl { public TabDetail() { InitializeComponent(); } //根目錄的名字 public static readonly DependencyProperty TRootNameProperty = DependencyProperty.Register("TRootName", typeof(string), typeof(TabDetail), new PropertyMetadata(default(string))); public string TRootName { get { return (string)GetValue(TRootNameProperty); } set { SetValue(TRootNameProperty, value); } } //定義ListBox的資料來源 public static readonly DependencyProperty TIResourceProperty = DependencyProperty.Register("TIResource", typeof(IList<NavigateDetail>), typeof(TabDetail), new PropertyMetadata(default(IList<NavigateDetail>))); public IList<NavigateDetail> TIResource { get { return (IList<NavigateDetail>)GetValue(TIResourceProperty); } set { SetValue(TIResourceProperty, value); } } //定義Grid的高 public static readonly DependencyProperty TItemActualHeightProperty = DependencyProperty.Register("TItemActualHeight", typeof(double), typeof(TabDetail), new PropertyMetadata(default(double))); public double TItemActualHeight { get { return (double)GetValue(TItemActualHeightProperty); } set { SetValue(TItemActualHeightProperty, value*TIResource.Count+5); } } //根目錄的樣式 public static readonly DependencyProperty TRootStyleProperty = DependencyProperty.Register("TRootStyle", typeof(Style), typeof(TabDetail), new PropertyMetadata(default(Style))); public Style TRootStyle { get { return (Style)GetValue(TRootStyleProperty); } set { SetValue(TRootStyleProperty, value); } } //根目錄上面的字型樣式 public static readonly DependencyProperty TTabDetailTextProperty = DependencyProperty.Register("TTabDetailText", typeof(Style), typeof(TabDetail), new PropertyMetadata(default(Style))); public Style TTabDetailText { get { return (Style)GetValue(TTabDetailTextProperty); } set { SetValue(TTabDetailTextProperty, value); } } private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Grid grid = sender as Grid; this.TListBox.SelectedIndex = -1; if (grid != null) { if (grid.Tag.ToString() == "0") { this.TListBoxOut.Begin(); grid.Tag = "1"; } else { this.TListBoxIn.Begin(); grid.Tag = "0"; } } } }}
在其他地方調用的時候
<uc:LeftMenu x:Name="LeftMenu" Grid.Column="0" AllResource="{StaticResource DList}" ItemActualHeight="25" RootStyle="{StaticResource LeftMenuStyle}" LeftMenuBg="{StaticResource LeftMenuBg}" LeftMenuText="{StaticResource LeftMenuText}"></uc:LeftMenu>
相關的樣式
<Style x:Key="LeftMenuStyle" TargetType="Grid"> <Setter Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="1,0"> <GradientStop Color="#838181" Offset="0.0"/> <GradientStop Color="Black" Offset="1.8"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="Height" Value="30"/> </Style>
<Style x:Key="LeftMenuBg" TargetType="StackPanel"> <Setter Property="Background" Value="#5A5858"/> </Style>
<Style x:Key="LeftMenuText" TargetType="TextBlock"> <Setter Property="Foreground" Value="White"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="Margin" Value="10,0"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontSize" Value="18"/> <Setter Property="Cursor" Value="Hand"/> </Style>