Skip to content

Commit

Permalink
Listbox
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Mar 7, 2012
1 parent bb546fc commit a1f01a5
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 16 deletions.
47 changes: 47 additions & 0 deletions App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,53 @@
<local:LoadedOpacityConerter x:Key="LoadedOpacityConerter" />
<local:BoolAccentConverter x:Key="BoolAccentConverter" />

<Style TargetType="ScrollViewer">
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ScrollViewer">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ScrollStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Scrolling">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="VerticalScrollBar" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="HorizontalScrollBar" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NotScrolling">
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="VerticalCompression">
<VisualState x:Name="NoVerticalCompression"/>
<VisualState x:Name="CompressionTop"/>
<VisualState x:Name="CompressionBottom"/>
</VisualStateGroup>
<VisualStateGroup x:Name="HorizontalCompression">
<VisualState x:Name="NoHorizontalCompression"/>
<VisualState x:Name="CompressionLeft"/>
<VisualState x:Name="CompressionRight"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="{TemplateBinding Padding}">
<ScrollContentPresenter x:Name="ScrollContentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
<ScrollBar x:Name="VerticalScrollBar" IsHitTestVisible="False" Opacity="0" Height="Auto" Width="5" HorizontalAlignment="Right" VerticalAlignment="Stretch" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Value="{TemplateBinding VerticalOffset}" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}" />
<ScrollBar x:Name="HorizontalScrollBar" IsHitTestVisible="False" Opacity="0" Width="Auto" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Value="{TemplateBinding HorizontalOffset}" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<!--RichTextBlock默认样式-->
<Style TargetType="RichTextBox">
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" />
Expand Down
26 changes: 12 additions & 14 deletions BoardPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:local="clr-namespace:sbbs_client_wp7"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand Down Expand Up @@ -49,6 +50,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
Expand All @@ -58,20 +60,16 @@
</StackPanel>

<!--ContentPanel - place additional content here-->
<ScrollViewer Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<ListBox ItemsSource="{Binding Topics}" ItemTemplate="{StaticResource TopicDataTemplate}"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
SelectionChanged="Topic_Selected">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Visibility="Collapsed" Margin="-12 0" x:Name="LoadMore" Content="载入更多" Click="LoadMore_Click"/>
</StackPanel>
</ScrollViewer>
<local:ExtendedListBox ItemsSource="{Binding Topics}" ItemTemplate="{StaticResource TopicDataTemplate}"
SelectionChanged="Topic_Selected"
Grid.Row="1" Margin="12,0,12,0">
<local:ExtendedListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</local:ExtendedListBox.ItemContainerStyle>
</local:ExtendedListBox>
<Button Grid.Row="2" Visibility="Collapsed" Margin="-12 0" x:Name="LoadMore" Content="载入更多" Click="LoadMore_Click"/>
</Grid>

<phone:PhoneApplicationPage.ApplicationBar>
Expand Down
152 changes: 152 additions & 0 deletions Controls/ExtendedListBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
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 Microsoft.Phone.Controls;
using System.Windows.Controls.Primitives;
using System.Collections;

namespace sbbs_client_wp7
{
public class ExtendedListBox : ListBox
{
protected bool _isBouncy = false;
private bool alreadyHookedScrollEvents = false;

public ExtendedListBox()
{
this.Loaded += new RoutedEventHandler(ListBox_Loaded);
}

private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
ScrollBar sb = null;
ScrollViewer sv = null;
if (alreadyHookedScrollEvents)
return;

alreadyHookedScrollEvents = true;
this.AddHandler(ExtendedListBox.ManipulationCompletedEvent, (EventHandler<ManipulationCompletedEventArgs>)LB_ManipulationCompleted, true);
sb = (ScrollBar)FindElementRecursive(this, typeof(ScrollBar));
sv = (ScrollViewer)FindElementRecursive(this, typeof(ScrollViewer));

if (sv != null)
{
// Visual States are always on the first child of the control template
FrameworkElement element = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement;
if (element != null)
{
VisualStateGroup vgroup = FindVisualState(element, "VerticalCompression");
VisualStateGroup hgroup = FindVisualState(element, "HorizontalCompression");
if (vgroup != null)
vgroup.CurrentStateChanging += new EventHandler<VisualStateChangedEventArgs>(vgroup_CurrentStateChanging);
if (hgroup != null)
hgroup.CurrentStateChanging += new EventHandler<VisualStateChangedEventArgs>(hgroup_CurrentStateChanging);
}
}

}

public delegate void OnCompression(object sender, CompressionEventArgs e);
public event OnCompression Compression;

private void hgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e)
{
if (e.NewState.Name == "CompressionLeft")
{
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Left));
}

if (e.NewState.Name == "CompressionRight")
{
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Right));
}
if (e.NewState.Name == "NoHorizontalCompression")
{
_isBouncy = false;
}
}

private void vgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e)
{
if (e.NewState.Name == "CompressionTop")
{
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Top));
}
if (e.NewState.Name == "CompressionBottom")
{
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Bottom));
}
if (e.NewState.Name == "NoVerticalCompression")
_isBouncy = false;
}

private void LB_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
if (_isBouncy)
_isBouncy = false;
}

private UIElement FindElementRecursive(FrameworkElement parent, Type targetType)
{
int childCount = VisualTreeHelper.GetChildrenCount(parent);
UIElement returnElement = null;
if (childCount > 0)
{
for (int i = 0; i < childCount; i++)
{
Object element = VisualTreeHelper.GetChild(parent, i);
if (element.GetType() == targetType)
{
return element as UIElement;
}
else
{
returnElement = FindElementRecursive(VisualTreeHelper.GetChild(parent, i) as FrameworkElement, targetType);
}
}
}
return returnElement;
}

private VisualStateGroup FindVisualState(FrameworkElement element, string name)
{
if (element == null)
return null;

IList groups = VisualStateManager.GetVisualStateGroups(element);
foreach (VisualStateGroup group in groups)
if (group.Name == name)
return group;

return null;
}
}

public class CompressionEventArgs : EventArgs
{
public CompressionType Type { get; protected set; }

public CompressionEventArgs(CompressionType type)
{
Type = type;
}
}

public enum CompressionType { Top, Bottom, Left, Right };
}
4 changes: 2 additions & 2 deletions MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
<local:Tile Title="热门版面" Tap="HotBoards_Tap" Src="/Images/Tiles/list.png"/>
<local:Tile Title="版面分区" Tap="Sections_Tap" Src="/Images/Tiles/folder.png"/>
<local:Tile Title="最近浏览" Tap="History_Tap" Src="/Images/Tiles/history.png"/>
<local:Tile Title="我的信箱" Tap="Mail_Tap" Src="/Images/Tiles/mailbox.png" Visibility="{Binding IsLogin, Converter={StaticResource BoolVisibleConverter}}"/>
<local:Tile Title="搜索" Src="/Images/Tiles/search.png"/>
<local:Tile Title="站内信" Tap="Mail_Tap" Src="/Images/Tiles/mailbox.png" Visibility="{Binding IsLogin, Converter={StaticResource BoolVisibleConverter}}"/>
<!--<local:Tile Title="搜索" Src="/Images/Tiles/search.png"/>-->
<local:Tile Title="注册" Src="/Images/Tiles/cup.png" Visibility="{Binding IsLogin, Converter={StaticResource BoolVisibleConverter}, ConverterParameter=True}"/>
<local:Tile Title="关于" Src="/Images/Tiles/love.png"/>
<local:Tile Title="退出" Src="/Images/Tiles/pen.png" Tap="Logout_Tap" Visibility="{Binding IsLogin, Converter={StaticResource BoolVisibleConverter}}"/>
Expand Down
1 change: 1 addition & 0 deletions sbbs-client-wp7.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="BoardSettingsPage.xaml.cs">
<DependentUpon>BoardSettingsPage.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\ExtendedListBox.cs" />
<Compile Include="DynamicOrientationChanges\AnimateOrientationChangesFrame.cs" />
<Compile Include="DynamicOrientationChanges\FadeOrientationChangesFrame.cs" />
<Compile Include="DynamicOrientationChanges\HybridOrientationChangesFrame.cs" />
Expand Down

0 comments on commit a1f01a5

Please sign in to comment.