I want to make a slider button using WPF. So I have this code to create a circle and to move it when clicked.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Border Background="LightGray" Margin="167,63,224,190" CornerRadius="20,20,20,20" Height="40" Width="80">
<Path Fill="Red">
<Path.Data>
<EllipseGeometry Center="20,20" RadiusX="20" RadiusY="20"/>
</Path.Data>
<Path.Effect>
<DropShadowEffect Direction="270" ShadowDepth="2" Color="Gray"></DropShadowEffect>
</Path.Effect>
<Path.Triggers>
<EventTrigger RoutedEvent="Border.MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<PointAnimation Storyboard.TargetProperty="Center" Duration="0:0:0.3" From="20,20" To="60,20"></PointAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Path.Triggers>
</Path>
</Border>
</Grid>
It is okey that it shows up when program started. But it throws an exception when clicked on it. The error is System.InvalidOperationException. So how can I fix this problem?
Perhaps this is something closer to what you are looking to do?
<Window x:Class="MainWindow"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Border Background="LightGray" Margin="167,63,224,190" CornerRadius="20,20,20,20" Height="40" Width="80">
<Canvas>
<Ellipse Fill="Red" Width="40" Height="40" Canvas.Left="0" Canvas.Top="0">
<Ellipse.Effect>
<DropShadowEffect Direction="270" ShadowDepth="2" Color="Gray"></DropShadowEffect>
</Ellipse.Effect>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Border.MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:0.3" From="0" To="40" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Canvas>
</Border>
</Grid>
Related
all. I have started a simple WPF application and would like a button to follow an elliptical path when it is made visible(for now I just want it to occur when I click...for testing purposes). It should simply move along the path then come to rest at the final point. I am having trouble getting the storyboard to work in a style. It won't reference the path that I have already defined. How do I resolve this? Eventually, each button will need to have it's own final resting point, so I will need to pass in an extra animation at a later date. The code is in XAML(posted below).
<Window x:Class="EllipseFollowsPath.MainWindow"
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"
xmlns:local="clr-namespace:EllipseFollowsPath"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1366">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="768" Width="1366">
<Grid.Resources>
<PathGeometry x:Key="path">
<PathGeometry.Figures>
<PathFigure StartPoint="921,384" IsClosed="False">
<ArcSegment Point="445,384" Size="60 60"
SweepDirection="Counterclockwise"/>
</PathFigure>
<PathFigure StartPoint="445,384" IsClosed="False">
<ArcSegment Point="921,384" Size="60 60"
SweepDirection="Counterclockwise"/>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
<Storyboard x:Key="sb" RepeatBehavior="Forever">
<MatrixAnimationUsingPath
Storyboard.TargetName="transform"
Storyboard.TargetProperty="Matrix"
PathGeometry="{StaticResource path}"
Duration="0:0:2">
</MatrixAnimationUsingPath>
</Storyboard>
</Grid.Resources>
<Button Height="20" Width="20">
<Button.RenderTransform>
<MatrixTransform x:Name="transform"/>
</Button.RenderTransform>
<Button.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard Storyboard="{StaticResource sb}">
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
The code below works...
<Window x:Class="EllipseFollowsPath.MainWindow"
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"
xmlns:local="clr-namespace:EllipseFollowsPath"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1366">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="768" Width="1366">
<Grid.Resources>
<PathGeometry x:Key="path">
<PathGeometry.Figures>
<PathFigure StartPoint="921,384" IsClosed="False">
<ArcSegment Point="445,384" Size="60 60"
SweepDirection="Counterclockwise"/>
</PathFigure>
<PathFigure StartPoint="445,384" IsClosed="False">
<ArcSegment Point="921,384" Size="60 60"
SweepDirection="Counterclockwise"/>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
<Storyboard x:Key="sb" RepeatBehavior="Forever">
<MatrixAnimationUsingPath
Storyboard.TargetName="transform"
Storyboard.TargetProperty="Matrix"
PathGeometry="{StaticResource path}"
Duration="0:0:2">
</MatrixAnimationUsingPath>
</Storyboard>
</Grid.Resources>
<Button Height="20" Width="20">
<Button.RenderTransform>
<MatrixTransform x:Name="transform"/>
</Button.RenderTransform>
<Button.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard Storyboard="{StaticResource sb}">
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
I wanna Move or Change position of an Image by clicking Another component.
I know How to change an Image location by clicking it. This code shows.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Image">
<Style.Triggers>
<EventTrigger RoutedEvent="MouseLeftButtonUp">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="(RenderTransform).(TranslateTransform.Y)"
Duration="0:0:2"
To="-279">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Canvas Margin="10">
<Rectangle Fill="Blue"
Height="150"
Width="150" Canvas.Left="269" Canvas.Top="139">
</Rectangle>
<Image x:Name="image" Source="Images/circle_yellow.png" Height="100" Canvas.Left="50" Canvas.Top="162" Width="100">
<Image.RenderTransform>
<TranslateTransform/>
</Image.RenderTransform>
</Image>
</Canvas>
</Grid>
But I wanna be Able to click the rectangle, not the Image. and Make the Image Move
Try this:
<Canvas Margin="10">
<Rectangle Fill="Blue"
Height="150"
Width="150" Canvas.Left="269" Canvas.Top="139">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="MouseLeftButtonUp">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="image"
Storyboard.TargetProperty="(RenderTransform).(TranslateTransform.Y)"
Duration="0:0:2"
To="-279">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
<Image x:Name="image" Source="Images/circle_yellow.png" Height="100" Canvas.Left="50" Canvas.Top="162" Width="100">
<Image.RenderTransform>
<TranslateTransform/>
</Image.RenderTransform>
</Image>
</Canvas>
How can I achieve THIS type of animation? I'm very new to WPF.
I tried with DoubleAnimation in WPF but not achieved the target.
Animation should be happen when I update the score like in video it's happening when score is updating from 17 to 23.
Try this.
<Window x:Class="WpfApplication1.AnimWindow"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="AnimWindow" Height="300" Width="300">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PointsNew" To="1.0" Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" />
<DoubleAnimation Storyboard.TargetName="PointsOld" To="0.0" Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" />
<ThicknessAnimation Storyboard.TargetName="PointsNew" From="0 -32 0 0" To="0 0 0 0" Storyboard.TargetProperty="Margin" Duration="0:0:0.5" />
<ThicknessAnimation Storyboard.TargetName="PointsOld" To="0 32 0 0" Storyboard.TargetProperty="Margin" Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Grid>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0.0" x:Name="PointsNew">
<TextBlock Text="23" FontSize="96" FontWeight="Bold" />
</Border>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="1.0" x:Name="PointsOld">
<TextBlock Text="17" FontSize="96" FontWeight="Bold" />
</Border>
</Grid></Window>
Try to play with margin, duration, and so on! :-)
Please don't get daunted by the size of the code. It is a very small user control.
This user control is supposed to wave the text inside it (marquee) :
<UserControl x:Class="WpfTest.Marquee"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfTest"
mc:Ignorable="d"
d:DesignHeight="25" d:DesignWidth="50">
<UserControl.Resources>
<Storyboard x:Key="Storyboard1" AutoReverse="True" RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="label">
<EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="-100"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
</EventTrigger>
</UserControl.Triggers>
<Grid Width="50">
<StackPanel Width="1000">
<Label x:Name="label"
Content="{Binding Content, FallbackValue='a long long text to test'}"
RenderTransformOrigin="0.5,0.5" ClipToBounds="False"
>
<Label.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform X="-20" />
</TransformGroup>
</Label.RenderTransform>
</Label>
</StackPanel>
</Grid>
</UserControl>
When I use it without changing the content it works fine and animation works:
<Window x:Class="WpfTest.MainWindow"
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"
xmlns:local="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:Marquee></local:Marquee>
</Grid>
</Window>
But when I assign content to it the animation doesn't apply :
<Window x:Class="WpfTest.MainWindow"
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"
xmlns:local="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:Marquee Content="This is another long long text to test"></local:Marquee>
</Grid>
</Window>
Do you know how can I make it working after binding the content?
Say we have a standard WPF-application, and I want to show some UI-elements on the left side besides my main application window.
Is there a way to go "beyond the borders" of the window and show a visual element with buttons on the left besides my main window? If there is, can you point me to a tutorial / video / hint how to accomplish it?
Not sure if you're aiming to do all of this in code or in XAML, but using a Popup, you could do something like this?
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ButtonsOnPopup.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="400" Height="300">
<Window.Resources>
<Storyboard x:Key="OnMouseLeftButtonDown1">
<BooleanAnimationUsingKeyFrames BeginTime="0" Storyboard.TargetName="popup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnClick1">
<BooleanAnimationUsingKeyFrames BeginTime="0" Storyboard.TargetName="popup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
<DiscreteBooleanKeyFrame KeyTime="00:00:00.2" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="textBlock">
<BeginStoryboard Storyboard="{StaticResource OnMouseLeftButtonDown1}"/>
</EventTrigger>
<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
<BeginStoryboard x:Name="OnClick1_BeginStoryboard" Storyboard="{StaticResource OnClick1}"/>
</EventTrigger>
</Window.Triggers>
<Grid x:Name="LayoutRoot">
<Popup x:Name="popup" Placement="Left">
<StackPanel Background="White">
<TextBlock Text="Outside Window" TextWrapping="Wrap"/>
<Button x:Name="button" Width="75" Content="Close this"/>
</StackPanel>
</Popup>
<TextBlock x:Name="textBlock" HorizontalAlignment="Center" VerticalAlignment="Center" Text="MouseDown here" TextWrapping="Wrap" Background="#FFBFFFBD"/>
</Grid>
Hope this helps.
Popup maybe.
simple example:
<Window x:Class="WpfPopupTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Popup HorizontalOffset="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" IsOpen="True">
<StackPanel Background="HotPink">
<TextBlock Text="Hey from outside!" Foreground="Gold" />
<Button>Button!</Button>
</StackPanel>
</Popup>
</Grid>
Probably don't what you want to do, but i think that it could work for you.
You could use a non-modal dialog with no window border. It could then appear to be floating out in space if you want.
Cory