Click here to Skip to main content
15,895,011 members

Response to: One Challenge in WPF Programing

Revision 2
When you create a user control, you should of course provide some interface to its functionality, how else?

You can always expose some of its children, but this would violate encapsulation pretty badly, so don't do it. Instead, add internal (to be used in the same assembly) or public (to be used outside declaring assembly as well) properties and events of your user control. In the implementation, use the private child controls, but don't expose them directly. As simple as that.

[EDIT]

For example:

C#
public class MyUserContol {

    public MyUserControl() {
        //...
        someButton.Click += (sender, evenArgs) => {
            if (ButtonClickHandler != null)
               ButtonClickHandler.Invoke(this, new System.EventArgs());
            // or
            // ButtonClickHandler.Invoke(this.someButton, new System.EventArgs());
        }; // this is how a private event instance is delegated to an exposed one
        // ...
    } //MyUserControl
    
    Button someButton = new Button();
    GridView gridRoot = new GridView();

    internal event System.EventHandler ButtonClickHandler;
    internal object GridRootSelected {
        get { return this.gridRoot.SelectedValue; }
    } //GridRootSelected
   
    // and so on...

} //class MyUserContol

—SA
Posted 31-Jan-13 9:58am by Sergey Alexandrovich Kryukov.
Tags: ,