Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A pretty simple way to do this, would be to have a command fire from each of the buttons, whenever they are being clicked. The new page number that should be selected, could be sent to the command by passing a parameter to it.</p> <p>In the Window we can create a binding to the command, so it receives notifications when a button is clicked, and we can then select the new page in the ListBox, which causes the animation to run.</p> <p>I have written some code that demonstrates this, and just adds a little to what you have already:</p> <p>view1.xaml</p> <pre><code>&lt;UserControl x:Class="FirstTry.view1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:FirstTry" Height="300" Width="300"&gt; &lt;Grid Background="LightBlue"&gt; &lt;TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view1" /&gt; &lt;!-- Button fires command with the value 1 as next pageindex --&gt; &lt;Button Name="show2" Content="show second xaml" VerticalAlignment="Center" Width="150" Command="{x:Static local:Commands.SlideToPage}" CommandParameter="1"&gt;&lt;/Button&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>view2.xaml</p> <pre><code>&lt;UserControl x:Class="FirstTry.view2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:FirstTry" Height="300" Width="300"&gt; &lt;Grid Background="LightCoral"&gt; &lt;TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view2"/&gt; &lt;!-- Button fires command with the value 2 as next pageindex --&gt; &lt;Button Name="show3" Content="show third xaml" VerticalAlignment="Center" Width="150" Command="{x:Static local:Commands.SlideToPage}" CommandParameter="2 &lt;/Button&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>view3.xaml</p> <pre><code>&lt;UserControl x:Class="FirstTry.view3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:FirstTry" Height="300" Width="300"&gt; &lt;Grid Background="LightGreen"&gt; &lt;TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view3"/&gt; &lt;!-- Button fires command with the value 0 as next pageindex --&gt; &lt;Button Name="show1" Content="show first xaml" VerticalAlignment="Center" Width="150" Command="{x:Static local:Commands.SlideToPage}" CommandParameter="0"&gt;&lt;/Button&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Window1.xaml</p> <pre><code>&lt;Window x:Class="FirstTry.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:FirstTry" Title="MainWindow" SizeToContent="WidthAndHeight" ResizeMode="NoResize"&gt; &lt;Window.CommandBindings&gt; &lt;!-- Make the window listen for the SlideToPage command --&gt; &lt;CommandBinding Command="{x:Static local:Commands.SlideToPage}" CanExecute="SlideToPage_CanExecute" Executed="SlideToPage_Executed" /&gt; &lt;/Window.CommandBindings&gt; &lt;Window.Resources&gt; &lt;XmlDataProvider x:Key="views"&gt; &lt;x:XData&gt; &lt;Views xmlns=""&gt; &lt;View Title="View1"&gt; &lt;Page Source="view1.xaml"/&gt; &lt;/View&gt; &lt;View Title="View2"&gt; &lt;Page Source="view2.xaml"/&gt; &lt;/View&gt; &lt;View Title="View3"&gt; &lt;Page Source="view3.xaml"/&gt; &lt;/View&gt; &lt;/Views&gt; &lt;/x:XData&gt; &lt;/XmlDataProvider&gt; &lt;Storyboard x:Key="slideLeftToRight" TargetProperty="RenderTransform.(TranslateTransform.X)" AccelerationRatio=".4" DecelerationRatio=".4"&gt; &lt;DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="300" To="0"/&gt; &lt;DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="-300"/&gt; &lt;/Storyboard&gt; &lt;Storyboard x:Key="slideRightToLeft" TargetProperty="RenderTransform.(TranslateTransform.X)" AccelerationRatio=".4" DecelerationRatio=".4"&gt; &lt;DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="-300" To="0"/&gt; &lt;DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="300"/&gt; &lt;/Storyboard&gt; &lt;VisualBrush x:Key="VisualBrush1" Visual="{Binding ElementName=viewer}"/&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;ListBox x:Name="viewList" Height="20" Width="300" SelectedIndex="0" ItemsSource="{Binding Source={StaticResource views}, XPath=Views/View}" DisplayMemberPath="@Title" SelectionChanged="viewList_SelectionChanged"&gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;Grid Width="300" Height="300"&gt; &lt;Border x:Name="bordervisual" Width="300"&gt; &lt;Rectangle x:Name="rectanglevisual"/&gt; &lt;Border.RenderTransform&gt; &lt;TranslateTransform/&gt; &lt;/Border.RenderTransform&gt; &lt;/Border&gt; &lt;ItemsControl x:Name="viewer" DataContext="{Binding Path=SelectedItem, ElementName=viewList}" ItemsSource="{Binding XPath=Page}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Frame x:Name="frame" Source="{Binding XPath=@Source}"/&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;ItemsControl.RenderTransform&gt; &lt;TranslateTransform/&gt; &lt;/ItemsControl.RenderTransform&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Window1.xaml.cs</p> <pre><code>public partial class Window1 : Window { private static int oldSelectedIndex = 0; public Window1() { InitializeComponent(); } public RenderTargetBitmap RenderBitmap(FrameworkElement element) { double topLeft = 0; double topRight = 0; int width = (int)element.ActualWidth; int height = (int)element.ActualHeight; double dpiX = 96; // this is the magic number double dpiY = 96; // this is the magic number PixelFormat pixelFormat = PixelFormats.Default; VisualBrush elementBrush = new VisualBrush(element); DrawingVisual visual = new DrawingVisual(); DrawingContext dc = visual.RenderOpen(); dc.DrawRectangle(elementBrush, null, new Rect(topLeft, topRight, width, height)); dc.Close(); RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, dpiX, dpiY, pixelFormat); bitmap.Render(visual); return bitmap; } private void viewList_SelectionChanged(object sender, SelectionChangedEventArgs e) { XmlElement root = (XmlElement)viewer.DataContext; XmlNodeList xnl = root.SelectNodes("Page"); if (viewer.ActualHeight &gt; 0 &amp;&amp; viewer.ActualWidth &gt; 0) { RenderTargetBitmap rtb = RenderBitmap(viewer); rectanglevisual.Fill = new ImageBrush(BitmapFrame.Create(rtb)); } viewer.ItemsSource = xnl; if (oldSelectedIndex &lt; viewList.SelectedIndex) { viewer.BeginStoryboard((Storyboard)this.Resources["slideLeftToRight"]); } else { viewer.BeginStoryboard((Storyboard)this.Resources["slideRightToLeft"]); } oldSelectedIndex = viewList.SelectedIndex; } private void SlideToPage_CanExecute(object sender, CanExecuteRoutedEventArgs e) { //The command can always be executed e.CanExecute = true; } private void SlideToPage_Executed(object sender, ExecutedRoutedEventArgs e) { //When the command is executed, we find the new pagenumber passed as a parameter //and switch the selected item of the ListBox, so the animation will run. int pagenumber = 0; if (int.TryParse(e.Parameter.ToString(), out pagenumber)) { viewList.SelectedIndex = pagenumber; } } } </code></pre> <p>Commands.cs</p> <pre><code>using System; using System.Windows.Input; namespace FirstTry { public static class Commands { public static readonly RoutedCommand SlideToPage = new RoutedCommand(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload