locked
Dependency Property Issue RRS feed

  • Question

  • I have UserControl , what i want is to create a dependency property for this UserControl on Visibility changed. Can anyone send me exact code for this? App is based on Silverlight 4, WCF RIA.

     

     

    Saturday, March 19, 2011 6:01 AM

Answers

  • Ah, I see. You can do the following in your user control:

    public partial class YourUserControl : UserControl
    {
        public new Visibility Visibility
        {
            get
            {
                return base.Visibility;
            }
            set
            {
                base.Visibility = value;
    
                // add your logic here, like raising an event etc.
            }
        }


    Saturday, March 19, 2011 9:36 AM

All replies

  • Hi Hammad. Visibility already is a dependency property, you don't have to (re-)create it manually. Can you explain what you want to do?

    Saturday, March 19, 2011 6:10 AM
  • On Change of UserControl Visibility i want to fire some event so that i perform some operation.

     

    Saturday, March 19, 2011 7:55 AM
  • Ah, I see. You can do the following in your user control:

    public partial class YourUserControl : UserControl
    {
        public new Visibility Visibility
        {
            get
            {
                return base.Visibility;
            }
            set
            {
                base.Visibility = value;
    
                // add your logic here, like raising an event etc.
            }
        }


    Saturday, March 19, 2011 9:36 AM
  • Thanks for the example code in your post MisterGoodcat. One thing I've noticed by taking this approach is that this Visibility override doesn't get called when the Visibility property is toggle by a VisualState.

    If I set the Visibility property directly on an instance of MyUserControl, the property setter is called correctly. 

    <Controls:MyUserControl x:Name="MyUserControl" Visibility="Collapsed"/>

    When I do it with a VisualState, it isn't, but MyUserControl still toggles Visibility correctly. 

    <VisualState x:Name="MyState">
         <Storyboard>
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="MyUserControl">
                  <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                                 <Visibility>Visible</Visibility>
                       </DiscreteObjectKeyFrame.Value>
                 </DiscreteObjectKeyFrame>
             </ObjectAnimationUsingKeyFrames>
         </Storyboard>
     </VisualState>

    I'm left to think there's another Visibility method that the VisualState is affecting change on.
    Could you give any pointers as to how I could work around this?

    Wednesday, September 21, 2011 8:06 AM
  • That is because the visual states do not use the CLR property wrapper, but work with the dependency properties directly. There's no easy way around this in Silverlight (unlike WPF). Here is a sample solution, but use this with care as I believe it might cause memory leaks in certain situations.

    Wednesday, September 21, 2011 10:16 AM