Live Chat Icon For mobile
Live Chat Icon

WPF FAQ - Serialization and Storage

Find answers for the most frequently asked questions
Expand All Collapse All

BorderThickness can be animated by using the ‘ThicknessAnimation’ class.

The following example animates the thickness of a border by using the ThicknessAnimation. The example uses the ‘BorderThickness’ property of Border.


<!-- This example shows how to use the ThicknessAnimation to create
an animation on the BorderThickness property of a Border. -->
<Page  xmlns=''
  xmlns:x='' >

  <StackPanel Orientation='Vertical' HorizontalAlignment='Center'>
    <Border Background='#99FFFFFF' BorderBrush='#CCCCFF' BorderThickness='1'
    Margin='0,60,0,20' Padding='20'  >
        <EventTrigger RoutedEvent='Border.Loaded'>

              <!-- BorderThickness animates from left=1, right=1, top=1, and bottom=1 to
              left=28, right=28, top=14, and bottom=14 over one second. -->
                Duration='0:0:1.5' FillBehavior='HoldEnd' From='1,1,1,1' To='28,14,28,14' />
        This example shows how to use the ThicknessAnimation to create
        an animation on the BorderThickness property of a Border.

To make the window display without this gray border, you need to set the ‘ResizeMode’ property of the window to ’NoResize’.

<Window x:Class='BorderlessWindow.Window1'
 Title='BorderlessWindow' Height='200' Width='200' 
 Background='White' WindowStyle='None' ResizeMode='NoResize'>
 <Border Padding='5' BorderBrush='#feca00' 
	 BorderThickness='3' Width='150' Height='150'>
 <TextBlock>Learn WPF!</TextBlock>

Versioning can be done using serialization as follows.

public class MyClass : ISerializable
   public int Number;
   public int NewField;
   public void GetObjectData(
       SerializationInfo info,StreamingContext context)

   protected MyClass(SerializationInfo info,StreamingContext context)
      Number = info.GetInt32('Number');
      Version storedVersion = SerializationUtil.GetVersion(info);
      if(storedVersion.ToString() == '')
         NewField = info.GetInt32('NewField');
         NewField = 123;//Some default value

   public MyClass()

public static class SerializationUtil
   static public Version GetVersion(SerializationInfo info)
      string assemblyName = info.AssemblyName;
      /* AssemblyName is in the form of 'MyAssembly, Version=,  
                              Culture=neutral,PublicKeyToken=null' */
      char[] separators = {’,’,’=’};
      string[] nameParts = assemblyName.Split(separators);
      return new Version(nameParts[2]);
   //Rest of SerializationUtil

This can be done as follows.


public static class SerializationUtil
   static public T Clone(T source)

      IGenericFormatter formatter = new GenericBinaryFormatter(); 
      Stream stream = new MemoryStream(); 

      T clone = formatter.Deserialize(stream);
      return clone;
//Rest of SerializationUtil

You can store a given document format using CreateSerializerWriter method as follows.


// Create a SerializerProvider for accessing plug-in serializers.
SerializerProvider serializerProvider = new SerializerProvider();

// Locate the serializer that matches the fileName extension.
SerializerDescriptor selectedPlugIn = null;
foreach ( SerializerDescriptor serializerDescriptor in
                serializerProvider.InstalledSerializers )
    if ( serializerDescriptor.IsLoadable &&
         fileName.EndsWith(serializerDescriptor.DefaultFileExtension) )
    {   // The plug-in serializer and fileName extensions match.
        selectedPlugIn = serializerDescriptor;
        break; // foreach

// If a match for a plug-in serializer was found,
// use it to output and store the document.
if (selectedPlugIn != null)
    Stream package = File.Create(fileName);
    SerializerWriter serializerWriter =
    IDocumentPaginatorSource idoc =
        flowDocument as IDocumentPaginatorSource;
    serializerWriter.Write(idoc.DocumentPaginator, null);
    return true;

The following example uses controllable storyboard actions to interactively control a storyboard.


<Page xmlns=''
  WindowTitle='Controlling a Storyboard' >
  <StackPanel Margin='20' >

    <!-- This rectangle is animated. -->
    <Rectangle Name='myRectangle'
      Width='100' Height='20' Margin='12,0,0,5' Fill='#AA3333FF' HorizontalAlignment='Left' />

    <!-- This StackPanel contains all the Buttons. -->
    <StackPanel Orientation='Horizontal' Margin='0,30,0,0'>

      <Button Name='BeginButton'>Begin</Button>
      <Button Name='PauseButton'>Pause</Button>
      <Button Name='ResumeButton'>Resume</Button>
      <Button Name='SeekButton'>Seek</Button>
      <Button Name='SkipToFillButton'>Skip To Fill</Button>
      <Button Name='SetSpeedRatioButton'>Triple Speed</Button>
      <Button Name='StopButton'>Stop</Button>


        <!-- Begin the Storyboard -->
        <EventTrigger RoutedEvent='Button.Click' SourceName='BeginButton'>
          <BeginStoryboard Name='MyBeginStoryboard'>
            <Storyboard >
                Duration='0:0:5' From='100' To='500' />

        <!-- Pause the Storyboard -->
        <EventTrigger RoutedEvent='Button.Click' SourceName='PauseButton'>
          <PauseStoryboard BeginStoryboardName='MyBeginStoryboard' />

        <!-- Resume the Storyboard -->
        <EventTrigger RoutedEvent='Button.Click' SourceName='ResumeButton'>
          <ResumeStoryboard BeginStoryboardName='MyBeginStoryboard' />

         <!-- Seek one second into the storyboard’s active period. -->
         <EventTrigger RoutedEvent='Button.Click' SourceName='SeekButton'>
            Offset='0:0:1' Origin='BeginTime' />

        <!-- Skip to Fill -->
        <EventTrigger RoutedEvent='Button.Click' SourceName='SkipToFillButton'>
          <SkipStoryboardToFill BeginStoryboardName='MyBeginStoryboard' />

        <!-- Stop the Storyboard -->
        <EventTrigger RoutedEvent='Button.Click' SourceName='StopButton'>
          <StopStoryboard BeginStoryboardName='MyBeginStoryboard' />

        <!-- Triple the speed of the Storyboard -->
        <EventTrigger RoutedEvent='Button.Click' SourceName='SetSpeedRatioButton'>
          <SetStoryboardSpeedRatio SpeedRatio='3' BeginStoryboardName='MyBeginStoryboard' />

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.