Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for FlipView : Navigation's button visibility and Transition update #2750

Merged
merged 11 commits into from
Nov 22, 2016
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mx="clr-namespace:MetroDemo.Markup"
xmlns:vc="clr-namespace:MetroDemo.ValueConverter"
mc:Ignorable="d"
d:DesignHeight="600"
d:DesignWidth="800"
Expand All @@ -20,7 +22,7 @@
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="TextOptions.TextRenderingMode" Value="ClearType" />
</Style>

<DataTemplate x:Key="ImageDataTemplate" x:Shared="False">
<Image Stretch="Fill" Source="{Binding Mode=OneWay, FallbackValue={x:Static DependencyProperty.UnsetValue}}" />
</DataTemplate>
Expand Down Expand Up @@ -110,11 +112,13 @@
<Grid Grid.IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Group1" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<CheckBox x:Name="ShowBannerCheckBox"
Grid.Column="0"
Expand All @@ -139,13 +143,19 @@
Margin="5 2"
Content="Show NaviButtons"
IsChecked="True" />
<ComboBox x:Name="Orientation"
ItemsSource="{Binding Source={mx:EnumBindingSource {x:Type Orientation}}, Mode=OneTime}"
SelectedIndex="0"
Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"/>
</Grid>
<Controls:FlipView Height="200"
<Controls:FlipView x:Name="FlipView1st"
Height="200"
Margin="0 0 5 0"
IsBannerEnabled="{Binding ElementName=ShowBannerCheckBox, Path=IsChecked, Mode=OneWay}"
MouseOverGlowEnabled="{Binding ElementName=ShowMouseOverCheckBox, Path=IsChecked, Mode=OneWay}"
CircularNavigation="{Binding ElementName=CircularNaviCheckBox, Path=IsChecked, Mode=OneWay}"
IsNavigationEnabled="{Binding ElementName=ShowNaviCheckBox, Path=IsChecked, Mode=OneWay}"
Orientation="{Binding ElementName=Orientation, Path=SelectedValue, Mode=OneWay}"
SelectionChanged="FlipView_SelectionChanged">
<Controls:FlipView.Items>
<Grid Background="#2E8DEF">
Expand All @@ -171,18 +181,42 @@
</Grid>
</Controls:FlipView.Items>
</Controls:FlipView>
<StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=FlipView1st, Mode=OneWay}" HorizontalAlignment="Center">
<Controls:NumericUpDown Controls:TextBoxHelper.SelectAllOnFocus="True"
ButtonsAlignment="Left"
Interval="1"
Minimum="1"
Maximum="{Binding Path=Items.Count, Mode=OneWay}"
Value="{Binding Path=SelectedIndex, Mode=TwoWay, Converter={vc:Int32IndexToNumberConverter}}"/>
<TextBlock VerticalAlignment="Center" Margin="8 0 0 0">
<Run Text="/" />
<Run Text="{Binding Path=Items.Count, Mode=OneWay}" />
</TextBlock>
</StackPanel>

<Separator Margin="0 5" />

<Controls:FlipView Height="200"
<Controls:FlipView x:Name="FlipView2nd" Height="200"
Margin="0 0 5 0"
BannerText="Databound Items"
IsBannerEnabled="{Binding ElementName=ShowBannerCheckBox, Path=IsChecked, Mode=OneWay}"
MouseOverGlowEnabled="{Binding ElementName=ShowMouseOverCheckBox, Path=IsChecked, Mode=OneWay}"
CircularNavigation="{Binding ElementName=CircularNaviCheckBox, Path=IsChecked, Mode=OneWay}"
IsNavigationEnabled="{Binding ElementName=ShowNaviCheckBox, Path=IsChecked, Mode=OneWay}"
Orientation="{Binding ElementName=Orientation, Path=SelectedValue, Mode=OneWay}"
ItemTemplate="{StaticResource ImageDataTemplate}"
ItemsSource="{Binding FlipViewImages, Mode=OneWay}" />
<StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=FlipView2nd, Mode=OneWay}" HorizontalAlignment="Center">
<Controls:NumericUpDown Controls:TextBoxHelper.SelectAllOnFocus="True"
ButtonsAlignment="Left"
Interval="1"
Minimum="1"
Maximum="{Binding Path=Items.Count, Mode=OneWay}"
Value="{Binding Path=SelectedIndex, Mode=TwoWay, Converter={vc:Int32IndexToNumberConverter}}"/>
<TextBlock VerticalAlignment="Center" Margin="8 0 0 0">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not starting from 0? Then you don't need the Int32IndexToNumberConverter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right.
It's just that users tend to prefer message 'Viewing item 1 on 2' than 'Viewing item 0 on 1'.
Hence the shift of 1.

<Run Text="/" />
<Run Text="{Binding Path=Items.Count, Mode=OneWay}" />
</TextBlock>
</StackPanel>
</StackPanel>

<Grid Grid.Row="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void Tick(object sender, EventArgs e)
private void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var flipview = ((FlipView)sender);
switch (flipview.SelectedIndex) {
switch (flipview.SelectedIndex)
{
case 0:
flipview.BannerText = "Cupcakes!";
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@
<DependentUpon>VSDemo.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)MainWindowViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Markup\EnumBindingSourceExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Navigation\HomePage.xaml.cs">
<DependentUpon>HomePage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Navigation\InterestingPage.xaml.cs">
<DependentUpon>InterestingPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ValueConverter\AlbumPriceIsTooMuchConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValueConverter\Int32IndexToNumberConverter.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="$(MSBuildThisFileDirectory)App.xaml">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Windows.Markup;

namespace MetroDemo.Markup
{

/// <summary>
/// Markup extension for Enum values.
/// </summary>
public class EnumBindingSourceExtension
: MarkupExtension
{
private Type _EnumType;

/// <summary>
/// Gets or sets the type of the Enum.
/// </summary>
/// <exception cref="ArgumentException">Value is not an Enum type.</exception>
public Type EnumType
{
get { return this._EnumType; }
set
{
if (!Object.Equals(value, this.EnumType))
{
if (!Object.Equals(value, null) && !(Nullable.GetUnderlyingType(value) ?? value).IsEnum)
throw new ArgumentException("Type must be an Enum.");

this._EnumType = value;
}
}
}

/// <summary>
/// Initializes a new instance of EnumBindingSourceExtension.
/// </summary>
public EnumBindingSourceExtension() { }

/// <summary>
/// Initializes a new instance of EnumBindingSourceExtension.
/// </summary>
/// <param name="enumType">The type of the Enum.</param>
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}

/// <summary>
///
/// </summary>
/// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
/// <returns>The values of the Enum.</returns>
/// <exception cref="InvalidOperationException">The type of the Enum is undefined.</exception>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (Object.Equals(this.EnumType, null))
throw new InvalidOperationException("The type of the Enum is undefined.");

var underlyingEnumType = Nullable.GetUnderlyingType(this.EnumType) ?? this.EnumType;
var enumValues = Enum.GetValues(underlyingEnumType);
if (underlyingEnumType.Equals(this.EnumType))
return enumValues;

var nullableEnumValues = Array.CreateInstance(underlyingEnumType, enumValues.Length);
enumValues.CopyTo(nullableEnumValues, 1);
return nullableEnumValues;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Data;
using System.Windows.Markup;

namespace MetroDemo.ValueConverter
{
/// <summary>
/// Converts an Int32 zero-based index to a one-based number.
/// </summary>
public class Int32IndexToNumberConverter
: MarkupExtension , IValueConverter
{
/// <summary>
/// Returns the value for the target property of this markup extension.
/// </summary>
/// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
/// <returns>Reference to the instance of this Int32IndexToNumberConverter.</returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}

/// <summary>
/// Converts an Int32 zero-based index to a one-based number.
/// </summary>
/// <param name="value">Int32 zero-based index.</param>
/// <param name="targetType">Ignored.</param>
/// <param name="parameter">Ignored.</param>
/// <param name="culture">Ignored.</param>
/// <returns>Int32 one-based number.</returns>
/// <exception cref="FormatException">Incorrect format.</exception>
/// <exception cref="InvalidCastException">Unsupported convversion.</exception>
/// <exception cref="OverflowException">Out of range of Int32.</exception>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToInt32(value) + 1;
}

/// <summary>
/// Converts an Int32 one-based number to a zero-based index.
/// </summary>
/// <param name="value">Int32 one-based number.</param>
/// <param name="targetType">Ignored.</param>
/// <param name="parameter">Ignored.</param>
/// <param name="culture">Ignored.</param>
/// <returns>Int32 zero-based index.</returns>
/// <exception cref="FormatException">Incorrect format.</exception>
/// <exception cref="InvalidCastException">Unsupported convversion.</exception>
/// <exception cref="OverflowException">Out of range of Int32.</exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToInt32(value) - 1;
}
}
}
Loading