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>