6/8/11

cheap fast screen saver looking "under construction"

if a view isn't ready for "prime time" poking around but people still would like to have a peek - usually it's just easy to throw a transparent rectangle to make it a "read only" view. So, I was just bored and added a screen saver like animation to it. All it is, is a textbox "Under Construction" floating through the screen rotating. Totally half baked but made my QA/Product laugh.

   1:  <Canvas Background="#0C100F0F" x:Name="canvas" Margin="0,0,50,0">
   2:    <Canvas.Triggers>
   3:      <EventTrigger RoutedEvent="TextBlock.Loaded" SourceName="txt">
   4:        <EventTrigger.Actions>
   5:          <BeginStoryboard>
   6:            <Storyboard>
   7:              <DoubleAnimation Storyboard.TargetName="txt" 
   8:                           Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)"
   9:                                                   From="0" To="360" Duration="0:0:50" RepeatBehavior="Forever"/>
  10:                          <DoubleAnimation Storyboard.TargetName="txt" 
  11:                                                   Storyboard.TargetProperty="(Canvas.Left)" 
  12:                                                   From="-25" To="900" Duration="0:0:50" RepeatBehavior="Forever" />
  13:                          <DoubleAnimation Storyboard.TargetName="txt" 
  14:                                                   Storyboard.TargetProperty="(Canvas.Top)" 
  15:                                                   From="-25" To="1250" Duration="0:0:50" RepeatBehavior="Forever" />
  16:            </Storyboard>
  17:          </BeginStoryboard>
  18:              </EventTrigger.Actions>
  19:        </EventTrigger>
  20:    </Canvas.Triggers>
  21:      <TextBlock Name="txt" Text="under construction" Margin="50" Canvas.Left="550" Canvas.Top="450" 
  22:                            FontSize="75" Foreground="Black" Opacity="0.5" Cursor="Hand">
  23:        <TextBlock.RenderTransform>
  24:        <RotateTransform Angle="0" CenterX="75" CenterY="25" />
  25:          </TextBlock.RenderTransform>
  26:      </TextBlock>
  27:  </Canvas>

putting this canvas in a grid or whatever parent should do the trick. oh, adding some special key combination to unlock the view so that qa can test (and making them struggle to find what it is) can make a Friday after 3 p.m. lonely afternoon with tumbleweeds rolling through abandoned cubicals more exciting.
To make rotations cooler, I should add dependency properties in the back .cs that would bind to Canvas' Left.From, Left.To, Right.From, etc... and make sure they are generating new values. So that the rotation is random. But that's for the next boring afternoon.

5/31/11

Find Visual Parent & Child

Tired of writing these two methods over and over, from now on just going to Copy/Paste them from here :)


   1:   
   2:          static T FindVisualParent<T>(UIElement element) where T : UIElement
   3:          {
   4:              var parent = element;
   5:              while (parent != null)
   6:              {
   7:                  T correctlyTyped = parent as T;
   8:   
   9:                  if (correctlyTyped != null)
  10:                      return correctlyTyped;
  11:   
  12:                  parent = VisualTreeHelper.GetParent(parent) as UIElement;
  13:              }
  14:              return null;
  15:          }
  16:   
  17:          static T FindVisualChild<T>(Visual parent) where T : Visual
  18:          {
  19:              T child = default(T);
  20:              int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
  21:              for (int i = 0; i < numVisuals; i++)
  22:              {
  23:                  var visual = (Visual)VisualTreeHelper.GetChild(parent, i);
  24:   
  25:                  child = visual as T;
  26:                  if (child == null)
  27:                      child = FindVisualChild<T>(visual);
  28:   
  29:                  if (child != null)
  30:                      break;
  31:   
  32:              }
  33:              return child;
  34:          }
  35:      }
  36:      
  37:      //call samples:
  38:      var dataGrid = FindVisualParent<DataGrid>(cell);
  39:      var row = FindVisualParent<DataGridRow>(cell);
  40:      var textBox = FindVisualChild<TextBox>(e.EditingElement);

12/14/10

Debugging XAML Binding

Good tip of the day:

To debug XAML Binding you can add reference to Diagnostics from WindowsBase dll to the XAML file, 
then when binding to some property add PresentationTraceSources.TraceLevel. When you run it check out 
the output window.


<TextBlock Text="{Binding someProperty, diagnostics:PresentationTraceSources.TraceLevel=High}"/>