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:
public class MyUserContol {
public MyUserControl() {
someButton.Click += (sender, evenArgs) => {
if (ButtonClickHandler != null)
ButtonClickHandler.Invoke(this, new System.EventArgs());
};
}
Button someButton = new Button();
GridView gridRoot = new GridView();
internal event System.EventHandler ButtonClickHandler;
internal object GridRootSelected {
get { return this.gridRoot.SelectedValue; }
}
}
—SA
Posted 31-Jan-13 9:58am
by
Sergey Alexandrovich Kryukov.