Good time, Heshkodovtsy.

Began to learn WPF, reached the triggers. I try to solve several problems on my own, set by myself, because The examples in the book are very simple, and I want something more real for use in life.

Actually, the first task is to animate the window display. I want that when the window is displayed on the screen, it smoothly leaves from outside the screen, here I have several problems:

  • I can not understand where the trigger shove. I try in the section <Window> </ Window>, I swear that it is impossible here, because it already exists (but I don’t observe it in the code). I tried in the Grid - here I swear that it cannot contain a trigger ... I also had an idea to put it in style, but with them I still haven’t quite clear everything.
  • How to get the screen size to pre-position the window depending on the screen resolution (I want the window on the right to crawl out). Well, for now, quite an example will come down so that the window on the left crawls out. Set constant values ​​on the left.

Thanks in advance for your answers.

    1 answer 1

    Crutches:

    The window stop position is set as a percentage of the screen width (WindowPosition, 30 in this case).

    In this line:

    xmlns:local="clr-namespace:wpf_test" 

    "wpf_test" will need to be replaced with the type of your form.

     <Window x:Class="wpf_test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:wpf_test" Title="MainWindow" Height="350" Width="525" x:Name="MySlidingWindow"> <Window.Resources> <sys:Int32 x:Key="WindowPosition">30</sys:Int32> <local:WindowPositionConverter x:Key="WindowPosConv" /> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="Loaded" SourceName="MySlidingWindow"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="MySlidingWindow" Storyboard.TargetProperty="Left" From="{x:Static SystemParameters.FullPrimaryScreenWidth}" To="{Binding Source={StaticResource WindowPosition}, Converter={StaticResource WindowPosConv}}" Duration="0:0:1.5"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Window.Triggers> <Grid> </Grid> </Window> 

    Further, the converter code:

     using System; using System.Globalization; using System.Windows.Data; namespace wpf_test { class WindowPositionConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var pos = (int) value; var result = System.Windows.SystemParameters.PrimaryScreenWidth/100*pos; return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } } 

    Here is such a sheet of code.