tags:

views:

138

answers:

3

Someone suggested this for me to use as an animation:

<Window x:Class="WpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="600">
<Window.Resources>
    <Storyboard x:Key="ScaleImageStoryboard">
        <DoubleAnimation Duration="0:0:0.2" From="1" To="1.2" AutoReverse="True"
                         Storyboard.TargetName="ScaleImage" Storyboard.TargetProperty="ScaleX"/>
        <DoubleAnimation Duration="0:0:0.2" From="1" To="1.2" AutoReverse="True" 
                         Storyboard.TargetName="ScaleImage" Storyboard.TargetProperty="ScaleY"/>
    </Storyboard>
</Window.Resources>
<Grid>
    <Image Name="Image" Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg" 
           Stretch="Fill" Width="300" Height="300"
           RenderTransformOrigin="0.5, 0.5">
        <Image.RenderTransform>
            <ScaleTransform x:Name="ScaleImage"/>
        </Image.RenderTransform>
        <Image.Triggers>
            <EventTrigger RoutedEvent="Image.MouseDown">
                <BeginStoryboard Storyboard="{StaticResource ScaleImageStoryboard}"/>
            </EventTrigger>
        </Image.Triggers>
    </Image>
</Grid>

Note that a single Image declaration in XAML is more than 6 lines! :D Is there a way for me to create much much cleaner XAML without breaking this functionality?

Thanks?

A: 

For situations like this is probably better to creating objects in source code ("old school way"), but if You prefer XML you can try use XSLT to generate it (but I don't recommend it).

Maciek Sawicki
You might also be able to put some of the settings you have shown above into a style to achieve the desired effect.
smaclell
What do you mean in the old school way? You mean declare the control in C# in the .cs file? How would I go about positioning the control where I want it, and won't that cause the application to delay a bit?
Sergio Tapia
Yes this is exactly what I mean. I don't think it cause delay. That objects must be crated anyway. You can control your layout from .cs file. http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx there is C# example of adding elements to Grid Children collection.
Maciek Sawicki
I don't see how putting the creation in the code behind is cleaner than creating a style in the xaml.
daub815
You can store objects in some collection, and initialize them in loop.
Maciek Sawicki
+6  A: 

I think the easiest solution here is to create a style for the image, where you define the triggers and storyboard

<Window.Resources>

    <Style x:Key="imageStyle" TargetType="{x:Type Image}">

        <Setter Property="RenderTransform">
            <Setter.Value>
                <ScaleTransform />
            </Setter.Value>
        </Setter>

        <Setter Property="RenderTransformOrigin" Value="0.5, 0.5" />

        <Style.Triggers>
            <EventTrigger RoutedEvent="Image.MouseDown">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:0.2" From="1" To="1.2" AutoReverse="True"
                                     Storyboard.TargetProperty="RenderTransform.ScaleX"/>
                        <DoubleAnimation Duration="0:0:0.2" From="1" To="1.2" AutoReverse="True" 
                                     Storyboard.TargetProperty="RenderTransform.ScaleY"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>

    </Style>

</Window.Resources>

You can then use this style for all your images :

<Image Name="Image"
       Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg" 
       Stretch="Fill" Width="300" Height="300"
       Style="{StaticResource imageStyle}" />

Note : I didn't test it, it might require a few modifications...

Thomas Levesque
(Before seeing your sample) This seems like the logical approach. Can't wait to learn. Thanks a million. :D
Sergio Tapia
That's just beautiful code right there. :) I'll try it out, but I'm certain it'll work. By the way Thomas, can I set style for every type of control? For instance, if I want a textbox to be black, I can declare the Style? Thanks a bunch.
Sergio Tapia
Just fixed it, it should work fine now...
Thomas Levesque
+2  A: 

I believe that Thomas' answer is quite good. If that's still to much code for you, you can make some collection of strings (or more sophisticated objects representing your 60 images), make it available for your Window through some property and display it using ItemsControl and appropriate data template (and panel [ItemsControl.ItemsPanel property] if you want to do some fancy positioning). There's really no need for 'old shool way' in WPF ;).

<Window>
  <ItemsControl ItemsSource={Binding ListOfPaths}>
    <ItemsControl.ItemTemplate>
       <DataTemplate>
         <Image Name="Image"
            Source="{Binding}" 
            Stretch="Fill" Width="300" Height="300"
            Style="{StaticResource imageStyle}" />
       </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</Window>
cz_dl