locked
How to access methods in variable usercontrol

    Question

  • Say I have 2 usercontrols "UC1" and "UC2", each has a public method with same name.  In the container page, I have

    UserControl[] UC = new UserControl[3];

    Depending on what the user does, the page will add either UC1 or UC2 to the stackpanel.

    UC[i] = new UC1();
    
    or 
    
    UC[i] = new UC2();
    This works wonderfully.  Unfortunately, I can't access the methods in the usercontrols.  Anyway I can go around this?

    Wednesday, April 30, 2014 6:54 PM

Answers

  • You can't expose the a field from an interface. You'd either have to put it on a shared parent class or expose the field as a property. It would be a cleaner encapsulation to expose an event at the UserControl's level rather than having the UserControl's consumers explicitly touch the TextBox. In most cases you'd be best off exposing a string property with the TextBox's contents and binding the containing page to that property rather than listening for text events.

    See Custom events in the Windows Runtime

    I'm sure you already know how to create a CLR property, but just in case here's a read only property to expose a TextBox. Also check out the Visual Studios templates for property creation. Type "prop<tab><tab>" in the designer and it will insert template code for you. It's pretty boring for a straight property, but the "propa" and "propd" versions are great for attached and dependency properties respectively.

            public TextBox SpiffyTextBox 
            {
                get 
                { 
                    return 
                        _textBox; 
                }
            }
    

    Thursday, May 1, 2014 6:03 PM
    Owner

All replies

  • Declare the joint methods as an interface which both UserControls can then implement. To call it, cast the variables referencing the UserControls back to the interface

        interface IMyInterface
        {
            bool MyFunction(string arg);
        }
        public sealed partial class MyUserControl1 : UserControl, IMyInterface
        {
            public MyUserControl1()
            {
                this.InitializeComponent();
            }
    
            public bool MyFunction(string arg)
            {
                throw new NotImplementedException();
            }
        }
    
        public sealed partial class MyUserControl2 : UserControl, IMyInterface
        {
            public MyUserControl2()
            {
                this.InitializeComponent();
            }
    
            public bool MyFunction(string arg)
            {
                throw new NotImplementedException();
            }
        }

                UserControl muc1 = new MyUserControl1();
                UserControl muc2 = new MyUserControl2();
    
                IMyInterface mif = muc1 as IMyInterface;
                if (mif != null)
                    mif.MyFunction("calling MyUserControl1's MyFunction");
                mif = muc2 as IMyInterface;
                if (mif != null)
                    mif.MyFunction("calling MyUserControl2's MyFunction");
                
    --Rob

    Wednesday, April 30, 2014 7:10 PM
    Owner
  • Thank you for your answer. 

    What about fields like textboxes?  I have the textboxes set to public.  I need the containing page to detect textchanges in these fields.

    Wednesday, April 30, 2014 11:21 PM
  • You can't expose the a field from an interface. You'd either have to put it on a shared parent class or expose the field as a property. It would be a cleaner encapsulation to expose an event at the UserControl's level rather than having the UserControl's consumers explicitly touch the TextBox. In most cases you'd be best off exposing a string property with the TextBox's contents and binding the containing page to that property rather than listening for text events.

    See Custom events in the Windows Runtime

    I'm sure you already know how to create a CLR property, but just in case here's a read only property to expose a TextBox. Also check out the Visual Studios templates for property creation. Type "prop<tab><tab>" in the designer and it will insert template code for you. It's pretty boring for a straight property, but the "propa" and "propd" versions are great for attached and dependency properties respectively.

            public TextBox SpiffyTextBox 
            {
                get 
                { 
                    return 
                        _textBox; 
                }
            }
    

    Thursday, May 1, 2014 6:03 PM
    Owner