Jammers (Hopefully) Useful C# WPF Silverlight Code Snippets

On this page I'm going to slowly organise useful little bits of code that I either write or find (will obviously credit the authors) just so that I or anyone that finds this page can quickly find useful things.

Animations

Fade Extension Method - WPF

        public static void Fade(this UIElement control, double sourceOpacity, double targetOpactity, int milliseconds)

        {

            control.BeginAnimation(UIElement.OpacityProperty,

                                   new DoubleAnimation(sourceOpacity, targetOpactity,

                                   new Duration(TimeSpan.FromMilliseconds(milliseconds))));

        }

Fade Extension Method - Silverlight

        public static void Fade(this FrameworkElement control, double sourceOpacity, double targetOpactity, int milliseconds)

        {

            var doubleAnimation = new DoubleAnimation

                                      {

                                          From = sourceOpacity,

                                          To = targetOpactity,

                                          Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))

                                      };

 

            control.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);

        }


Generics

Generic List with Fluent Interface

using System.Collections.Generic;

 

namespace JamSoft.Mvvm.ToyBox.Core

{

    public class FluentList<T> : List<T>

    {

        public new FluentList<T> Add(T obj)

        {

            base.Add(obj);

            return this;

        }

 

        public new FluentList<T> AddRange(IEnumerable<T> objs)

        {

            base.AddRange(objs);

            return this;

        }

    }

}

With this little wrapper around a normal List you can do this:

            var stringList = new FluentList<string>()

                .Add("Moon")

                .Add("Alpha Centauri")

                .Add("Sirius");

Number Crunching

Is Even Number

        public static bool IsEven(int intValue)

        {

            return ((intValue % 2) == 0);

        }

spacer graphic
Jammer footer graphic