I have a problem with an animantion of an UserControl.
The animation runs only once.
If I have multiple instances of my UserControl in another window, the animation is called for every Instance of the UserControl.
<UserControl x:Class="My_UserControl"
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"
mc:Ignorable="d" Margin="10,10">
<UserControl.Resources>
<Storyboard x:Key="CartTicket_Whip">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)" Storyboard.TargetName="UserControlGrid">
<SplineDoubleKeyFrame KeyTime="0:0:0.1" Value="-2"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsHitTestVisible)" Storyboard.TargetName="UserControlGrid">
<DiscreteBooleanKeyFrame KeyTime="0:0:0.1" Value="True"/>
<DiscreteBooleanKeyFrame KeyTime="0:0:0.2" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="DecrementButton">
<BeginStoryboard Storyboard="{StaticResource CartTicket_Whip}"/>
</EventTrigger>
</UserControl.Triggers>
<Grid x:Name="UserControlGrid" Height="60">
<Grid.LayoutTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.LayoutTransform>
<Grid.Effect>
<DropShadowEffect ShadowDepth="5" Color="Silver"></DropShadowEffect>
</Grid.Effect>
<DockPanel Background="White">
<Button Name="DecrementButton" DockPanel.Dock="Left" Content="-" FontSize="40" Width="60">
</Button>
<TextBlock x:Name="TicketCount" DockPanel.Dock="Left" Text="2 x" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="20" FontWeight="Bold" Margin="10">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="4" Color="Silver"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
<StackPanel DockPanel.Dock="Left" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5">
<TextBlock x:Name="TicketName" Text="Ticket type 01" HorizontalAlignment="Center"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10,0,0">
<TextBlock x:Name="PriceTxt" Grid.Column="2" Text="100" HorizontalAlignment="Center"/>
<TextBlock x:Name="PriceTxt_currency" Grid.Column="2" Text=" EUR" HorizontalAlignment="Center"/>
</StackPanel>
</StackPanel>
</DockPanel>
</Grid>
</UserControl>
You can set the FillBehavior property of your Storyboard to Stop in this way:
<UserControl.Resources>
<Storyboard x:Key="CartTicket_Whip" FillBehavior="Stop">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)" Storyboard.TargetName="UserControlGrid">
<SplineDoubleKeyFrame KeyTime="0:0:0.1" Value="-2"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsHitTestVisible)" Storyboard.TargetName="UserControlGrid">
<DiscreteBooleanKeyFrame KeyTime="0:0:0.1" Value="True"/>
<DiscreteBooleanKeyFrame KeyTime="0:0:0.2" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
I hope it helps.
Ok, I got it... sorry it is a dump coincidance! Because I changed the skew of the Grid element, to have a bob up and down effect, the hole usercontrol had this effect.
Now in my window I put five of this UserControls in a Grid, under each other...
So there was the effect, that on every click it seems that evrey UserControl started the animation...
So here the new code, where I make the storyboard for a child element (the dockPanel):
<UserControl x:Class="MyUserControl"
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"
mc:Ignorable="d" Margin="10,10">
<UserControl.Resources>
<Storyboard x:Key="CartTicket_Whip">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)" Storyboard.TargetName="UserControlDockPanel">
<SplineDoubleKeyFrame KeyTime="0:0:0.1" Value="-1.5"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="DecrementButton">
<BeginStoryboard Storyboard="{StaticResource CartTicket_Whip}"/>
</EventTrigger>
</UserControl.Triggers>
<Grid x:Name="UserControlGrid" Height="60">
<Grid.LayoutTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.LayoutTransform>
<Grid.Effect>
<DropShadowEffect ShadowDepth="5" Color="Silver"></DropShadowEffect>
</Grid.Effect>
<DockPanel x:Name="UserControlDockPanel" Background="White" RenderTransformOrigin="0.5,0.5">
<DockPanel.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</DockPanel.RenderTransform>
<Button Name="DecrementButton" DockPanel.Dock="Left" Content="-" FontSize="40" Width="60">
</Button>
<TextBlock x:Name="TicketCount" DockPanel.Dock="Left" Text="2 x" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="20" FontWeight="Bold" Margin="10">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="4" Color="Silver"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
<StackPanel DockPanel.Dock="Left" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5">
<TextBlock x:Name="TicketName" Text="Ticket type 01" HorizontalAlignment="Center"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10,0,0">
<TextBlock x:Name="PriceTxt" Grid.Column="2" Text="100" HorizontalAlignment="Center"/>
<TextBlock x:Name="PriceTxt_currency" Grid.Column="2" Text=" EUR" HorizontalAlignment="Center"/>
</StackPanel>
</StackPanel>
</DockPanel>
</Grid>
</UserControl>
Related
I wonder if it is possible to create a digital clock in xaml using animation (without background code tags)
The analog clock can be realized by converting the current time to the angle by matrix conversion, but the digital clock can't be operated like this. I tried a lot of methods, but it didn't work. Does anyone know if there is any good way to implement it?
Analog clock implementation
<Window>
<Window.Resources>
<FrameworkElement x:Key="time" Tag={x:Static s:DateTime.Now}/>
<TransformGroup x:Key="transformHour">
<TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Hour}"
Y="{Binding Source={StaticResource time},Path=Tag.Minute}"/>
<MatrixTransform Matrix="30 0 0.5 0 0 0"/>
</TransformGroup>
<TransformGroup x:Key="transformMinute">
<TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Minute}"
Y="{Binding Source={StaticResource time},Path=Tag.Second}"/>
<MatrixTransform Matrix="6 0 0.1 0 0 0"/>
</TransformGroup>
<TransformGroup x:Key="transformSecond">
<TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Second}"/>
<MatrixTransform Matrix="6 0 0 0 0 0"/>
</TransformGroup>
<Style TargetType="{x:Type Path}">
<Setter Property="Stroke"
Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="StrokeThickness" Value="3"/>
<Setter Property="StrokeDashCap" Value="Triangle"/>
</Style>
</Window.Resources>
<Viewbox>
<Canvas Width="200" Height="200">
<Canvas.RenderTransform>
<TranslateTransform X="100" Y="100"/>
</Canvas.RenderTransform>
<Path Data="M 0 -90 A 90 90 0 1 1 -0.01 -90"
StrokeDashArray="0 3.14157" />
<Path Data="M 0 -90 A 90 90 0 1 1 -0.01 -90"
StrokeDashArray="0 7.854"
StrokeThickness="6"/>
<Border Background="LightBlue" Width="10" Height="80" RenderTransformOrigin="0.5 0">
<Border.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="bor_Second" Angle="{Binding Source={StaticResource transformSecond},Path=Value.OffsetX}"/>
<RotateTransform Angle="180"/>
</TransformGroup>
</Border.RenderTransform>
</Border>
<Border Background="LightGreen" Width="10" Height="60" RenderTransformOrigin="0.5 0">
<Border.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="bor_Minute" Angle="{Binding Source={StaticResource transformMinute},Path=Value.OffsetX}"/>
<RotateTransform Angle="180"/>
</TransformGroup>
</Border.RenderTransform>
</Border>
<Border Background="LightGray" Width="10" Height="40" RenderTransformOrigin="0.5 0">
<Border.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="bor_Hour" Angle="{Binding Source={StaticResource transformHour},Path=Value.OffsetX}"/>
<RotateTransform Angle="180"/>
</TransformGroup>
</Border.RenderTransform>
</Border>
</Canvas>
</Viewbox>
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="bor_Hour"
Storyboard.TargetProperty="Angle"
IsAdditive="True"
Duration="12:0:0"
From="0" To="360"
RepeatBehavior="Forever"/>
<DoubleAnimation Storyboard.TargetName="bor_Minute"
Storyboard.TargetProperty="Angle"
IsAdditive="True"
Duration="1:0:0"
From="0" To="360"
RepeatBehavior="Forever"/>
<DoubleAnimation Storyboard.TargetName="bor_Second"
Storyboard.TargetProperty="Angle"
IsAdditive="True"
Duration="0:1:0"
From="0" To="360"
RepeatBehavior="Forever"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
</Window>
Digital clock (there is an error), I feel that it is more troublesome to use this idea. Does anyone know if there is any good way to implement it?
<Window>
<Window.Resources>
<!--current time-->
<FrameworkElement x:Key="time" Tag="{x:Static s:DateTime.Now}"/>
<!--Current minutes remaining seconds-->
<TransformGroup x:Key="transformSecond">
<TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Second}" Y="60"/>
<MatrixTransform Matrix="-1 0 1 0 0 0"/>
</TransformGroup>
<!--Remaining seconds interval-->
<FrameworkElement x:Key="timeSpanSecond"
Tag="{Binding Source={StaticResource transformSecond},Path=Value.OffsetX,StringFormat={}{0:F0}}"/>
<!--Current hours remaining minutes-->
<TransformGroup x:Key="transformMinute">
<TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Minute}" Y="60"/>
<MatrixTransform Matrix="-1 1 1 0 0 1"/>
</TransformGroup>
<!--Remaining minute interval-->
<FrameworkElement x:Key="timeSpanMinute"
Tag="{Binding Source={StaticResource transformMinute},Path=Value.OffsetX,StringFormat={}{0:F0}}"/>
<!--Next minute-->
<FrameworkElement x:Key="minuteNext"
Tag="{Binding Source={StaticResource transformMinute},Path=Value.OffsetY}"/>
<!--Hours remaining on the day-->
<TransformGroup x:Key="transformHour">
<TranslateTransform X="{Binding Source={StaticResource time},Path=Tag.Hour}" Y="24"/>
<MatrixTransform Matrix="-1 1 1 0 0 1"/>
</TransformGroup>
<!--Remaining hours interval-->
<FrameworkElement x:Key="timeSpanHour"
Tag="{Binding Source={StaticResource transformHour},Path=Value.OffsetX,StringFormat={}{0:F0}}"/>
<!--Next hour-->
<FrameworkElement x:Key="hourNext"
Tag="{Binding Source={StaticResource transformHour},Path=Value.OffsetY}"/>
</Window.Resources>
<Grid>
<!--Width:Current seconds-->
<!--Text:Current remaining seconds(TimeSpan)-->
<TextBlock x:Name="tbk_Second" Visibility="Hidden"
Width="{Binding Source={StaticResource time},Path=Tag.Second}"
Text="{Binding Source={StaticResource timeSpanSecond},StringFormat=0:0:{0},Path=Tag}"/>
<TextBlock x:Name="tbk_Minute" Visibility="Hidden"
Width="{Binding Source={StaticResource time},Path=Tag.Minute}"
Text="{Binding Source={StaticResource timeSpanMinute},StringFormat=0:{0}:0,Path=Tag}">
</TextBlock>
<TextBlock x:Name="tbk_Hour" Visibility="Hidden"
Width="{Binding Source={StaticResource time},Path=Tag.Hour}"
Text="{Binding Source={StaticResource timeSpanHour},StringFormat={}{0}:0:0,Path=Tag}"/>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">
<Run Text="{Binding ElementName=tbk_Hour,Path=Width,StringFormat={}{0:F0}}"/>
<Run Text=":"/>
<Run Text="{Binding ElementName=tbk_Minute,Path=Width,StringFormat={}{0:F0}}"/>
<Run Text=":"/>
<Run Text="{Binding ElementName=tbk_Second,Path=Width,StringFormat={}{0:F0}}"/>
</TextBlock>
</Grid>
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation Storyboard.TargetName="tbk_Hour"
Storyboard.TargetProperty="Width"
BeginTime="{Binding ElementName=tbk_Minute,Path=Text}"
Duration="{Binding ElementName=tbk_Hour,Path=Text}"
From="{Binding Source={StaticResource hourNext},Path=Tag}"
To="23"/>
<DoubleAnimation Storyboard.TargetName="tbk_Hour"
Storyboard.TargetProperty="Width"
BeginTime="{Binding ElementName=tbk_Hour,Path=Text}"
Duration="24:0:0"
From="0"
To="23"
RepeatBehavior="Forever"/>
<DoubleAnimation Storyboard.TargetName="tbk_Minute"
Storyboard.TargetProperty="Width"
BeginTime="{Binding ElementName=tbk_Second,Path=Text}"
Duration="{Binding ElementName=tbk_Minute,Path=Text}"
From="{Binding Source={StaticResource minuteNext},Path=Tag}"
To="59"/>
<DoubleAnimation Storyboard.TargetName="tbk_Minute"
Storyboard.TargetProperty="Width"
BeginTime="{Binding ElementName=tbk_Minute,Path=Text}"
Duration="1:0:0"
From="0"
To="59"
RepeatBehavior="Forever"/>
<DoubleAnimation
Storyboard.TargetName="tbk_Second"
Storyboard.TargetProperty="Width"
Duration="{Binding ElementName=tbk_Second,Path=Text}"
From="{Binding Source={StaticResource time},Path=Tag.Second}"
To="59"
/>
<DoubleAnimation Storyboard.TargetName="tbk_Second"
Storyboard.TargetProperty="Width"
BeginTime="{Binding ElementName=tbk_Second,Path=Text}"
Duration="0:1:0"
From="0"
To="59"
RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
</Window>
Maybe this is the answer you want :)
<Window x:Class="WpfApp6.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:system="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
<Grid.Resources>
<!--Set x: share to get the latest every time-->
<system:DateTime x:Key="DateTime"
x:Shared="False" />
<Storyboard x:Key="Storyboard">
<!--Use keyframe animation to update datetime -->
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="DataContext"
Duration="0:0:1"
RepeatBehavior="Forever"
AutoReverse="False">
<DiscreteObjectKeyFrame KeyTime="50%"
Value="{StaticResource DateTime}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<!--Get datetime from DataContext-->
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},Path=DataContext.Now}"
DataContext="{StaticResource DateTime}">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard Storyboard="{StaticResource Storyboard}" />
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>
</Window>
I recommend you do it based on textblocks and a suitable font like this one:
https://www.keshikan.net/fonts-e.html
Your textblocks are bound to propertys that could be set by an async function calling 'DateTime.Now();'
here is a XAML-Example:
<Border HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,40" Background="DarkGray" BorderThickness="1,1,0,0" BorderBrush="Gray">
<Grid>
<!-- Displays the actual time in red-->
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Opacity="0.6" Foreground="Red" FontSize="20" Text="{Binding Hour, FallbackValue=00, StringFormat=00}" FontFamily="DSEG7 Classic Mini" ></TextBlock>
<TextBlock Opacity="0.6" Foreground="Red" FontSize="20" FontFamily="DSEG7 Classic Mini" >:</TextBlock>
<TextBlock Opacity="0.6" Foreground="Red" FontSize="20" Text="{Binding Min, FallbackValue=00, StringFormat=00}" FontFamily="DSEG7 Classic Mini" ></TextBlock>
<TextBlock Opacity="0.6" Foreground="Red" FontSize="20" FontFamily="DSEG7 Classic Mini">:</TextBlock>
<TextBlock Opacity="0.6" Foreground="Red" FontSize="20" Text="{Binding Sec, FallbackValue=00, StringFormat=00}" FontFamily="DSEG7 Classic Mini" ></TextBlock>
</StackPanel>
<!-- Creates a Glow Effect -->
<StackPanel Orientation="Horizontal" Margin="2">
<StackPanel.Effect>
<BlurEffect KernelType="Gaussian" Radius="3"/>
</StackPanel.Effect>
<TextBlock Foreground="Red" FontSize="20" Text="{Binding Hour, FallbackValue=00, StringFormat=00}" FontFamily="DSEG7 Classic Mini" ></TextBlock>
<TextBlock Foreground="Red" FontSize="20" FontFamily="DSEG7 Classic Mini" >:</TextBlock>
<TextBlock Foreground="Red" FontSize="20" Text="{Binding Min, FallbackValue=00, StringFormat=00}" FontFamily="DSEG7 Classic Mini" ></TextBlock>
<TextBlock Foreground="Red" FontSize="20" FontFamily="DSEG7 Classic Mini">:</TextBlock>
<TextBlock Foreground="Red" FontSize="20" Text="{Binding Sec, FallbackValue=00, StringFormat=00}" FontFamily="DSEG7 Classic Mini" ></TextBlock>
</StackPanel>
<!-- Creates the effect of turned of segments -->
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Opacity="0.1" FontSize="20" Text="88" FontFamily="DSEG7 Classic Mini" ></TextBlock>
<TextBlock Opacity="0.1" FontSize="20" FontFamily="DSEG7 Classic Mini" >:</TextBlock>
<TextBlock Opacity="0.1" FontSize="20" Text="88" FontFamily="DSEG7 Classic Mini" ></TextBlock>
<TextBlock Opacity="0.1" FontSize="20" FontFamily="DSEG7 Classic Mini">:</TextBlock>
<TextBlock Opacity="0.1" FontSize="20" Text="88" FontFamily="DSEG7 Classic Mini" ></TextBlock>
</StackPanel>
</Grid>
</Border>
it would be even easier using only one textblock and a string format but this example I already had lying around :)
I am using WPF NotifyIcon and using this Balloon:
<UserControl
x:Class="PacketPlayer.ApplicationBalloon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
Height="130"
Width="283"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:my="clr-namespace:PacketPlayer.classes"
mc:Ignorable="d"
x:Name="me">
<UserControl.Resources>
<Storyboard
x:Key="FadeInAndOut">
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="grid"
Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame
KeyTime="00:00:00"
Value="0" />
<SplineDoubleKeyFrame
KeyTime="00:00:01"
Value="0.895" />
<SplineDoubleKeyFrame
KeyTime="00:00:10"
Value="0.895" />
<SplineDoubleKeyFrame
KeyTime="00:00:11.6000000"
Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger
RoutedEvent="tb:TaskbarIcon.BalloonShowing">
<BeginStoryboard
Storyboard="{StaticResource FadeInAndOut}"
x:Name="FadeInAndOut_BeginStoryboard" />
</EventTrigger>
</UserControl.Triggers>
<Grid
x:Name="grid">
<Border
x:Name="border"
CornerRadius="10,10,10,10"
Margin="0,0,5,5">
<Border.Background>
<LinearGradientBrush
EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop
Color="#FFEEEEEE"
Offset="1" />
<GradientStop
Color="#FF474E5B"
Offset="0" />
</LinearGradientBrush>
</Border.Background>
<Border.Effect>
<DropShadowEffect />
</Border.Effect>
</Border>
<StackPanel Orientation="Vertical" Margin="10,10,0,0">
<TextBlock
VerticalAlignment="Top"
FontSize="18"
FontWeight="Bold"
TextWrapping="Wrap"
Text="Packet Player still running..."
Foreground="White"
Margin="0,0,0,0"/>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<ProgressBar
Name="progressBar"
Width="180"
BorderBrush="White"
Value="0"
Height="20"
Foreground="Yellow"
Background="Transparent"
HorizontalAlignment="Left"
Margin="0,12,0,0"/>
<TextBlock
Name="tbProgressBarPercent"
Text="0%"
Foreground="White"
FontSize="16"
Margin="7,11,0,0"/>
</StackPanel>
<Label
ContentStringFormat="{}{0:#,0} Packets sent"
HorizontalAlignment="Left"
Foreground="White"
Margin="-5,8,0,0"/>
</StackPanel>
</Grid>
</UserControl>
Inside the Balloon i put simple Progress Bar and change it's value via code behond.
<ProgressBar
Name="progressBar"
Width="180"
Height="25"
BorderBrush="Black"
Value="24"
Background="Transpare
Foreground="Yellow"/>
progressBar.Value = "33;
The only issue i have is the i can change all Progress Bar properties exept Forecolor the doing nothing.
This question already has answers here:
Detect mouse directly over border in WPF
(2 answers)
Closed 8 years ago.
I build a small control with a panel of two buttons. When I hover the control I want to show the panel and hide it when I leave. Everything works great, but I have one bug. When I move the cursor from the TextBlock to the Image my Storyboard run again. I dont understand why because both of them are inside the control. I try to add a Border to wrap the control, but it didnt work.
<UserControl.Resources>
<Storyboard x:Key="Show">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
Storyboard.TargetName="QuickPanel">
<EasingDoubleKeyFrame KeyTime="0" Value="18"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="Hide">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
Storyboard.TargetName="QuickPanel">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="18"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="Border.MouseEnter">
<BeginStoryboard Storyboard="{StaticResource Show}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Border.MouseLeave">
<BeginStoryboard Storyboard="{StaticResource Hide}"/>
</EventTrigger>
</UserControl.Triggers>
<Border Width="144" Height="36" ClipToBounds="True" BorderThickness="0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Source="http://www.streamplay.fr/thumb/fanart/3000/3000/1321396.jpg"
VerticalAlignment="Center" HorizontalAlignment="Center" Margin="2"
Stretch="UniformToFill" Height="34" Width="34"/>
<Grid Grid.Column="1">
<StackPanel VerticalAlignment="Center" Margin="6,0,0,0">
<TextBlock FontSize="12" FontWeight="DemiBold">Sample Title</TextBlock>
<TextBlock FontSize="10" Text="{Binding Title, Mode=TwoWay}"></TextBlock>
</StackPanel>
</Grid>
<StackPanel x:Name="QuickPanel" Grid.Column="1" Width="18" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5">
<StackPanel.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform X="18"/>
</TransformGroup>
</StackPanel.RenderTransform>
<Button x:Name="button" Height="18" Content="p" IsDefault="True" />
<Button x:Name="button1" Height="18" Content="k" />
</StackPanel>
</Grid>
</Border>
Any idea how to change this ?
You need to set a background to your Border element UI:
Border Background="Transparent" ...
If you don't set the background, the space between image and textblock is your parent window.
I am trying to animate a number of shapes within a visualbrush but when I perform a rotation the shapes 'pulse'. I am assuming that as the shapes rotate the bounding boxes are forcing a layout pass. However since I am using a RenderTransform I wasn't expecting this to trigger layout changes.
This code illustrates the problem:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="200" Width="200">
<StackPanel>
<Border BorderBrush="Red" BorderThickness="1"
Height="100" Width="100">
<Border.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="inner_Ellipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
<LinearDoubleKeyFrame KeyTime="0:0:3" Value="-360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="outer_Ellipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
<LinearDoubleKeyFrame KeyTime="0:0:3" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
<Border.Background>
<VisualBrush Stretch="Uniform">
<VisualBrush.Visual>
<Canvas Width="20" Height="20">
<Ellipse x:Name="outer_Ellipse"
Stroke="Blue" StrokeThickness="1"
Width="20" Height="20"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<RotateTransform/>
</Ellipse.RenderTransform>
</Ellipse>
<Ellipse x:Name="inner_Ellipse"
Stroke="Red" StrokeThickness="1"
Width="18" Height="18"
Margin="1,1,0,0"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<RotateTransform/>
</Ellipse.RenderTransform>
</Ellipse>
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
</Border>
</StackPanel>
This is a simple sample of a much more complicated application where I am using the Visual Brushes to decorate 2d planes being manipulated in 3d. It all works well until I try and animate the brushes. I have tried several different approaches but always seem to run into this layout issue.
Any suggestions appreciated.
Thanks
Rob
I was able to track down the cause of your problem. It has to do with the Stretch="Uniform" Property setting on your VisualBrush. It appears the framework is computing a bounding rectangle on your VisuaBrush.Visual, and then stretching it to fit Border.Background. The following code should illustrate the behavior. I took out your inner_Ellipse and added an outer_Rectangle that should simulate the bounding rectangle being stretched:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="200" Width="200">
<StackPanel>
<Border BorderBrush="Red" BorderThickness="1"
Height="100" Width="100">
<Border.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="outer_Rectangle"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
<LinearDoubleKeyFrame KeyTime="0:0:6" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="outer_Ellipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
<LinearDoubleKeyFrame KeyTime="0:0:6" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
<Border.Background>
<VisualBrush Stretch="Uniform">
<VisualBrush.Visual>
<Canvas Width="20" Height="20">
<Ellipse x:Name="outer_Ellipse"
Stroke="Blue" StrokeThickness="1"
Width="20" Height="20"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<RotateTransform/>
</Ellipse.RenderTransform>
</Ellipse>
<Rectangle x:Name="outer_Rectangle"
Stroke="Blue" StrokeThickness="1"
Width="20" Height="20"
RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform/>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
</Border>
</StackPanel>
</Window>
As to solving the problem, I am not sure. One way would be to use Stretch="None" on your VisualBrush, but that doesn't seem ideal because it then falls on you to deal with the size of your VisualBrush.Visual contents.
Well after a good deal of trial and error I have a working solution. The contents of the VisualBrush correctly scale and don't cause the bounding box problem:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight" >
<StackPanel>
<Border BorderBrush="Black" BorderThickness="1"
Height="400" Width="400">
<Border.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="inner_Ellipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
<LinearDoubleKeyFrame KeyTime="0:0:3" Value="-360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="outer_Ellipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
<LinearDoubleKeyFrame KeyTime="0:0:3" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
<Border.Background>
<VisualBrush Stretch="UniformToFill">
<VisualBrush.Visual>
<Border BorderBrush="Transparent" BorderThickness="1"
Width="200" Height="200">
<StackPanel Width="200" Height="200">
<Canvas>
<Rectangle x:Name="outer_Ellipse"
Stroke="Blue" StrokeThickness="1"
Width="20" Height="200"
Canvas.Left="40"
RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform/>
</Rectangle.RenderTransform>
</Rectangle>
<Ellipse x:Name="inner_Ellipse"
Stroke="Red" StrokeThickness="1"
StrokeDashArray="2"
Canvas.Top="30"
Canvas.Left="20"
Width="200" Height="200"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<RotateTransform/>
</Ellipse.RenderTransform>
</Ellipse>
</Canvas>
</StackPanel>
</Border>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
</Border>
</StackPanel>
</Window>
The 'secret' appears to be wrapping the canvas containing the brush contents in a StackPanel and wrapping this in a Border. (a Grid, DockPanel and WrapPanel will also work in place of the StackPanel).
<Border BorderBrush="Transparent" BorderThickness="1"
Width="200" Height="200">
<StackPanel Width="200" Height="200">
The Border must have a BorderBrush set and a BorderThickness. Also they must both have an explicit width and height. Here I have set values appropriate to the content so it scales correctly.
Without all 3 of these components the bounding box issue re occurs. Clearly something about the layout policies of the containers makes a difference but I have no idea what or why.
Not at all a satisfying solution and I would appreciate it if anyone can shine a light on what's going on here.
At least it works, both in the above demo and my main application, so far anyway!
Rob
I wonder how can i parametrize an image source inside an style, and this style is used inside another style. Let me try to be more clear.
I defined an style for buttons, the style has an image. Then i have another style. On that style, I have two buttons (i´ve applied the button style), and i want to able to change the image of each button. But i can´t get it work.
The first part of the code defines a style for a button the idea is to change the image as needed to represent an action.
This is my code:
<Style x:Key="InnerInscribirseButtonMatch" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOver">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="Blur_back">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="-0.666"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Blur_back">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0.705"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="contentPresenter">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="2.665"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="contentPresenter">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="2.999"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Blur_back">
<EasingColorKeyFrame KeyTime="0" Value="#FFE9E9A7"/>
<EasingColorKeyFrame KeyTime="0:0:0.1" Value="#FFF1F0DD"/>
</ColorAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="image">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="3"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="image">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="4"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Back">
<EasingColorKeyFrame KeyTime="0" Value="#FF356AA0"/>
<EasingColorKeyFrame KeyTime="0:0:0.1" Value="#FF1264B6"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid>
<Rectangle x:Name="Blur_back" RadiusY="5" RadiusX="5" Stroke="#FF3F4C6B" StrokeThickness="2" Fill="#FF5B8BBC" Margin="-4.5,-4" Opacity="0" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
<Rectangle.Effect>
<BlurEffect Radius="7"/>
</Rectangle.Effect>
</Rectangle>
<Rectangle x:Name="Back" RadiusY="5" RadiusX="5" Stroke="#FF3F4C6B" StrokeThickness="2" Fill="#FF356AA0"/>
<Rectangle x:Name="Glass" RadiusY="5" RadiusX="5" Stroke="#FF3F4C6B" StrokeThickness="2" Fill="White" Height="Auto" VerticalAlignment="Stretch" Opacity="0.15" Margin="0,0,0,61" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center" RecognizesAccessKey="True" VerticalAlignment="Bottom" RenderTransformOrigin="0.5,0.5" Margin="13.766,0,15.766,14.351" d:LayoutOverrides="Width">
<ContentPresenter.Effect>
<DropShadowEffect Opacity="0.77"/>
</ContentPresenter.Effect>
<ContentPresenter.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</ContentPresenter.RenderTransform>
</ContentPresenter>
<Image x:Name="image" Margin="12,3.5,17,22.5" RenderTransformOrigin="0.5,0.5" Width="64" Height="64" Source="apuntarlista.png">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseDown">
<BeginStoryboard x:Name="MouseOver_BeginStoryboard" Storyboard="{StaticResource MouseOver}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseUp">
<StopStoryboard BeginStoryboardName="MouseOver_BeginStoryboard"/>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<StopStoryboard BeginStoryboardName="MouseOver_BeginStoryboard"/>
</EventTrigger>
<Trigger Property="IsCancel" Value="False"/>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
well, this is the part i want to customize. The source part: Source="apuntarlista.png"
<Image x:Name="image" Margin="12,3.5,17,22.5" RenderTransformOrigin="0.5,0.5" Width="64" Height="64" Source="apuntarlista.png">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
here, you have the second style
<Style x:Key="ButtonStyleCambiable" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Height="130" Width="Auto" Margin="50,0,-315,0">
<Rectangle x:Name="background" Fill="{Binding BackgroundColor, FallbackValue=#FF932424}" RadiusY="15" RadiusX="15" Stroke="{Binding BorderBackgroundColor, FallbackValue=#FF954444}" StrokeThickness="3" Height="130" Width="375" Margin="0" HorizontalAlignment="Left" d:LayoutOverrides="HorizontalAlignment"/>
<Rectangle x:Name="white_glass" Fill="White" RadiusY="15" RadiusX="15" Stroke="{x:Null}" StrokeThickness="3" Opacity="0.07" Height="65" VerticalAlignment="Top" Width="375" Margin="0" HorizontalAlignment="Left" ClipToBounds="True" d:LayoutOverrides="HorizontalAlignment"/>
<TextBlock HorizontalAlignment="Left" Margin="13,13.901,0,0" TextWrapping="Wrap" Width="272" VerticalAlignment="Center" FontSize="48" FontFamily="Cambria" Foreground="White" Text="{Binding Description, FallbackValue=50/100}" FontWeight="Bold">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="3" Opacity="0.35" BlurRadius="13"/>
</TextBlock.Effect></TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="206,14.192,0,0" TextWrapping="Wrap" Width="122" VerticalAlignment="Top" FontSize="24" FontFamily="Cambria" Foreground="White" FontWeight="Bold" Text="En espera: " d:LayoutOverrides="HorizontalAlignment">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="3" Opacity="0.35" BlurRadius="13"/>
</TextBlock.Effect></TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="210,50.123,0,51.739" TextWrapping="Wrap" Width="109" VerticalAlignment="Stretch" FontSize="24" FontFamily="Cambria" Foreground="White" FontWeight="Bold" Text="Libres: " d:LayoutOverrides="HorizontalAlignment, Height">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="3" Opacity="0.35" BlurRadius="13"/>
</TextBlock.Effect></TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="292,51.123,0,50.739" TextWrapping="Wrap" Width="130" VerticalAlignment="Stretch" FontSize="24" FontFamily="Cambria" Foreground="White" FontWeight="Bold" Text="{Binding FreeSits, FallbackValue=10}" d:LayoutOverrides="HorizontalAlignment, Height">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="3" Opacity="0.35" BlurRadius="13"/>
</TextBlock.Effect></TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="329,15.192,0,0" TextWrapping="Wrap" Width="96" VerticalAlignment="Top" FontSize="24" FontFamily="Cambria" Foreground="White" FontWeight="Bold" Text="{Binding PeopleWaiting, FallbackValue=1}" d:LayoutOverrides="HorizontalAlignment">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="3" Opacity="0.35" BlurRadius="13"/>
</TextBlock.Effect></TextBlock>
<Button Content="Inscribir" HorizontalAlignment="Left" Margin="381,2,0,2" Style="{DynamicResource InnerInscribirseButtonMatch}" Width="129" FontFamily="Calibri" FontSize="24" Foreground="White" Height="Auto" VerticalAlignment="Stretch" Opacity="0.85"/>
<Button Content="Info" HorizontalAlignment="Left" Margin="518,2,0,2" Style="{DynamicResource InnerInscribirseButtonMatch}" Width="129" FontFamily="Calibri" FontSize="24" Foreground="White" Height="Auto" VerticalAlignment="Stretch" Opacity="0.85"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
there i have:
<Button Content="Inscribir" HorizontalAlignment="Left" Margin="381,2,0,2" Style="{DynamicResource InnerInscribirseButtonMatch}" Width="129" FontFamily="Calibri" FontSize="24" Foreground="White" Height="Auto" VerticalAlignment="Stretch" Opacity="0.85"/>
<Button Content="Info" HorizontalAlignment="Left" Margin="518,2,0,2" Style="{DynamicResource InnerInscribirseButtonMatch}" Width="129" FontFamily="Calibri" FontSize="24" Foreground="White" Height="Auto" VerticalAlignment="Stretch" Opacity="0.85"/>
as you can see both buttons use Style="{DynamicResource InnerInscribirseButtonMatch}"
but i want each one to have a diferent image.
How can i do that? I Hope to be clear.
Thanks in advance.
I think that you can define Button.DataContext property, for example, by string, and bind the Image.Source property with control DataContext.
<Button DataContext="apuntarlista.png"
Style="{DynamicResource InnerInscribirseButtonMatch}"
/>
<Button DataContext="apuntarlista2.png"
Style="{DynamicResource InnerInscribirseButtonMatch}"
/>
And in your inner style:
<Image x:Name="image"
RenderTransformOrigin="0.5,0.5"
Width="64" Height="64" Source="{Binding}">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
IMHO i would always try to not change the style so that is needs specific input. Thats what the Content is for and the ContentTemplate.
But of course this is difficult sometimes.
We use for these cases an AttachedProperty
So make a new static class name it for example ButtonIconService. give it an Attached Property named Icon
set it on your buttons like
<Button ButtonIconService.Icon="pathToIcon.png"
<Button ButtonIconService.Icon="pathToOtherIcon.png"
and bind it to your Image in your template.
<Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(ButtonInfoService.Icon)}}"
im not sure at the moments if the brackets around ButtonInfoService.Icon are needed?
Hope that helps.