Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How to get TranslateTransform without x:Name RSS

3 replies

Last post Aug 22, 2008 08:33 AM by ryanvs

(0)
  • ryanvs

    ryanvs

    Member

    2 Points

    5 Posts

    How to get TranslateTransform without x:Name

    Aug 22, 2008 07:39 AM | LINK

    Is it possible to a refernce in code for a TranslateTransform if it doesn't have an x:Name attribute after the UserControl has loaded? For example:

    <Ellipse x:Name="Ellipse01" 
      Height="25" Width="25"
      Canvas.Left="0" Canvas.Top="0"
      Fill="#FFEE3131" Stroke="#FF000000"
      RenderTransformOrigin="0.5,0.5">
      <Ellipse.RenderTransform>
        <TransformGroup>
          <ScaleTransform/>
          <SkewTransform/>
          <RotateTransform/>
          <TranslateTransform 
    	    x:Name="Ellipse01_Translate"
    		X="50" Y="50"/>
        </TransformGroup>
      </Ellipse.RenderTransform>
    </Ellipse>
    

    In this case I assigned a x:Name of "Ellipse01_Translate", but how can I get a reference without the x:Name? So far, I've only been able to get a reference if I assign an x:Name attribute. Otherwise the runtime Transform properties of the Shape only have the matrix transform and not the TransformGroup. All I have been able to use is FindName such as the following:

    TranslateTransform translate = (TranslateTransform) Ellipse01.FindName("Ellipse01_Translate");

    The idea is that I will pass the Shape to another class so I won't know the name of the transform and I may use multiple transforms such as rotation and scale. Thanks.

    FindName TranslateTransform

  • robhouweling

    robhouweling

    Contributor

    3308 Points

    565 Posts

    Re: How to get TranslateTransform without x:Name

    Aug 22, 2008 07:51 AM | LINK

    Try this:

    TranslateTransform translate;
    var renderTransform = Ellipse01.RenderTransform;
    if (renderTransform is TransformGroup)
    {
       
    TransformGroup transformGroup = (TransformGroup)renderTransform;
       
    foreach (Transform transform in transformGroup.Children)
        {
            if (transform is TranslateTransform)
            {
                translate = (
    TranslateTransform)transform;
            }
        }
    }

    (If this has answered your question, please click on mark as answer on this post)



    Cheers!

    Rob Houweling





    My blog
  • robhouweling

    robhouweling

    Contributor

    3308 Points

    565 Posts

    Re: How to get TranslateTransform without x:Name

    Aug 22, 2008 08:25 AM | LINK

    You could also create an extension method for this.
    Not tested thoroughly, but here's one that works in your situation:

    public static T GetTransformation<T>(this UIElement Element)
    {
        T translate =
    default(T);
       
    var renderTransform = Element.RenderTransform;
       
    if (renderTransform is TransformGroup)
        {
           
    TransformGroup transformGroup = (TransformGroup)renderTransform;
           
    foreach (var transform in transformGroup.Children)
            {
               
    if (transform is T)
                {
                    translate = (T)(
    object)transform;
                }
            }
        }
        return translate;
    }

    (If this has answered your question, please click on mark as answer on this post)



    Cheers!

    Rob Houweling





    My blog
  • ryanvs

    ryanvs

    Member

    2 Points

    5 Posts

    Re: How to get TranslateTransform without x:Name

    Aug 22, 2008 08:33 AM | LINK

    Excellent. That is awesome. Thanks.