Nell'attesa di qualche anima buona che mi può dare qualche dritta, ho fatto qualche progresso.
Prima di tutto sono riuscito a creare l'immagine e a metterla sul Canvas. Ho usato il codice seguente:
Uri myUri = new Uri("E:\\Andrea\\Pictures\\Bridge\\Carte\\PNG Medie\\8F.png");
PngBitmapDecoder decoder = new PngBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
Image card = new Image();
card.HorizontalAlignment = HorizontalAlignment.Left;
card.VerticalAlignment = VerticalAlignment.Top;
card.Width = 100;
card.Height = 150;
card.Source = bitmapSource;
card.Name = "CartaAggiunta";
card.Style = (Style)(mainWindow.Resources["MouseOverImageStyle"]);
mainCanvas.Children.Add(card);
Non so se è la via più breve, ma funziona.
Sono riuscito anche a collegare lo Style che ho creato nel quale si attiva una storyboard che sposta la carta di 30 pixel quando il mouse ci passa sopra.
Tutto funziona a meraviglia.
Ora, attraverso un comando - che mi piacerebbe fosse il click del mouse sulla carta, ma ho visto che Image non possiede l'evento OnClick - devo spostare la carta e ruotarla di 90°. L'ho fatto con il codice seguente:
TranslateTransform tT = new TranslateTransform(50.0, 50.0);
RotateTransform rT = new RotateTransform(90.0, 75.0, 125.0);
TransformGroup tG = new TransformGroup();
tG.Children.Add(tT);
tG.Children.Add(rT);
card.RenderTransform = tG;
L'immagine si sposta e ruota. Ma il bello viene adesso: ci passo sopra con il mouse e ... l'applicazione raggiunge la seguente eccezione:
System.InvalidOperationException non è stata gestita
Message="La proprietà '[Unknown]' non punta a un DependencyObject nel percorso '(0).(1).[3].(2)'."
Source="PresentationFramework"
Cosa è successo? Non capisco l'errore.
Immagino che per capire è necessario anche il codice XAML dello style legato all'evento OnMouseOver. Eccolo:
<Style x:Key="MouseOverImageStyle" TargetType="{x:Type Image}">
<Style.Resources>
<Storyboard x:Key="UpImageStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="-30"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="DownImageStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-30"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource DownImageStoryboard}" x:Name="DownImageStoryboard_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource UpImageStoryboard}" x:Name="UpImageStoryboard_BeginStoryboard"/>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
<Setter Property="RenderTransform">
<Setter.Value>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
Help