locked
Controls made in Code-behind file overwritten RRS feed

  • Question

  • I am using the 2.5 June Preview version of Expr.Blend.

    I have an UI item (say a rectangular block, named "MyRect").
    It was designed such a way that the Opacity changes (giving an animation effect) as we go along the timeline.
    But the visibility property is set as "Visible" throughout in the Timeline.

    Now, on a button click, i thought of hiding this object.
    To achieve that, wrote a Event Handler function for the Button object's "Click" event.
    In the event handler code (C#), modified the visibility property to "Hidden".

    i.e. MyRect.visibility = visibility.hidden;

    In the first run, on the button click, the rectangle is getting hidden.
    But when the animation is repeated (enabled the repeat mode),
    the rectangle is hidden right from the beginning, i.e. even before the button click.

    Sometimes, i also find that the change made to an attribute in Code-behind file is not getting set.
    This is confirmed by debugging in step mode.

    What is the reason for this?
    What should i do to control the visibility of an element as per my choice based on events?

    Thanks in advance!!
    Sudhakaran (Kaangeyan).

    Monday, October 13, 2008 4:34 PM

Answers

  • Hi Kaangeyan,

    I have faced many many frustrating situations where the code behind just couldn't hide or show any object. I found that any changes done in your storyboards (stored as XAML), takes precedence over your code behind, I have faced many scenarios with this issue, not sure if anyone from the WPF team can confirm this.

    For example if you set the visibility property of an object to hidden with code behind, and you have a storyboard where you explicitly set the visibility property to visible, the storyboard takes precedence, and your object will be still visible.

    My approach to dealing with this issue is to always use storyboards to control the hiding and showing of objects, then use code behind to trigger these storyboards whenever necessary. One thign to note is to make sure you explicitly define the visibility property of an element, e.g: even though the value of the visibility property = visible, set it to hidden and then visible again to ensure the XAML that defines the object's visibility property is generated.

    Please mark as answered if this answered your question, hopefully this would help others facing the same issue too :)

    -
    Monday, October 20, 2008 9:28 AM

All replies

  • Howdy,

    Once you animate an elements properties in a timeline, you can not modify the property value until the animations fillbehaviour is set to Stop.. the animation is still over riding the properties base value.

    Have a look here for some more information on FillBehavior:

    http://msdn.microsoft.com/en-us/library/aa970493.aspx

    HTH

    Cheers

    Expression MVP

    Learn Expression Blend at learnexpressionstudio.com

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Tuesday, October 14, 2008 11:17 PM
    Moderator
  • Hi Kaangeyan,

    I have faced many many frustrating situations where the code behind just couldn't hide or show any object. I found that any changes done in your storyboards (stored as XAML), takes precedence over your code behind, I have faced many scenarios with this issue, not sure if anyone from the WPF team can confirm this.

    For example if you set the visibility property of an object to hidden with code behind, and you have a storyboard where you explicitly set the visibility property to visible, the storyboard takes precedence, and your object will be still visible.

    My approach to dealing with this issue is to always use storyboards to control the hiding and showing of objects, then use code behind to trigger these storyboards whenever necessary. One thign to note is to make sure you explicitly define the visibility property of an element, e.g: even though the value of the visibility property = visible, set it to hidden and then visible again to ensure the XAML that defines the object's visibility property is generated.

    Please mark as answered if this answered your question, hopefully this would help others facing the same issue too :)

    -
    Monday, October 20, 2008 9:28 AM
  • Hi KokChiann,

    It's not frustrating if you understand what is happening.

    Follow the link in my previous post and you should be able to work through it all. Setting property states to visible and then hidden and then back to visible is not the correct approach to take at all. Control your storyboards correctly and you will not have the issue.

    HTH

    Cheers

    Expression MVP

    Learn Expression Blend at learnexpressionstudio.com

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Monday, October 20, 2008 12:24 PM
    Moderator