Prevent User Control's constituent controls from appearing in the controls collection - c#

I have a control that contains another control. This child control is constructed and then displayed on the parent control by adding it to the parent's Controls collection.
However, this results in the consumers of the parent control being able to directly access the child control's properties.
How can I prevent this?
I did try hiding the Controls property but all you need do to avoid that is cast the parent control to type Control and the Controls property is accessible again. Unfortunately the Controls property doesn't appear to be virtual either.
I just seems a little odd, from an encapsulation point of view, to allow everyone external consumer access to the internals of the control.

Related

Determine at run time if a nested control is a design time container

Follow up question to my previous question
The 'TabControl' appears to work in a slightly odd way.
You have the 'TabControl' itself which is not a container, but a composite of several 'TabPage' controls which are containers.
My initial thought was, if the main control is not a container then iterate the child controls and recursively call my IsContainerControl function. However, this returns true for 'Panel' and 'GroupBox' controls that are child controls of both usercontrols and subclassed standard controls. I also tried checking for a Designer attribute on the child controls but these are present on in both cases.
Note that in these cases, the ContainerControl style set but the IDE does not allow controls to be dropped into them because they are not sited directly on the form. The question now becomes, what is the IDE checking on the TabPage controls to determine that you should be able to drop other controls on them?

C# WPF .NET 4 - Call parent function from user control

In my C# WPF .NET 4.0 application, I have a listbox containing user control items. What I want is to call some functions from these user controls to the parent form.
Item user controls have binding to the listbox via view model class.
What do you propose.
Thank you,
You can bind a command to your user control the same way as it would be a parent view. Then you can process this command in the appropriate view model.
I'm not entirely clear on what you're trying to do, but it sounds like you have a bunch of user controls and you want those controls to be able to call methods on the containing Window instance.
From inside your controls, you can use the following to get hold of the Window instance:
Window parentWindow = Window.GetWindow(this);
Note though that you can't do this from the control's constructors, because during the constructor the control won't yet have a parent window and the above will return null. The best place to do this would be from the control's Initialized or Loaded events.

How does the WP7 Pivot control dynamically load pivot items?

IIRC, the Pivot control only loads a child PivotItem if it is the currently shown child. I would then guess that the previously seen child is also somehow unloaded, presumably still stored in memory, but hidden from the UI.
What I'm wondering is, how does the Pivot control dynamically load/unload a child control, and can that behavior be imitated within a custom UserControl? As for unloading, is it as simple as collapsing the previous child's visibility, or is something trickier going on?
That is to say, supposing I use my own UserControl like:
<my:CustomUserControl>
<TextBlock x:Name="_textBlock" Text="wait for it ..." />
</my:CustomUserControl>
Normally, the child TextBlock is instantiated when the surrounding PhoneApplicationPage is instantiated, via InitializeComponent and all that. Is there any way to postpone this behavior and load the child programmatically?
Easy way to achive your goal is to use ContentControl with template. It will create all controls only after setting Contenp property.
Or you can simply inherit your control from Panel and add child controls in code.

How to change controls layout in child user controls

I have a user control implemented for a windows application. I have derived this user control and created one more child user control. But the problem is I am not able to change the control layout in child user control, being everything is locked.
How to change the layout of controls in child user control keeping the functionality same as parent user control ?
If I change the modifiers of controls in parent control to public, I can change the layout. But I am not sure if it is a correct way or not.
Thanks,
Vijay
I think you should try keeping the controls as protected instead of public.

WPF Event to capture newly added controls?

We use the ContentControl and other containers stuff in WPF. I need the notification with the new child control is added to the container. What is the best way to get the newly created control within parent?
The ContentControl only contains a single child which is attached via the ContentControl.Content property. You can hook the ContentControl.OnContentChanged to discover when the value of this property is updated.
The cleanest way is to derive from those control and override the methods that report the changes you are interested in. For example derive from ContentControl and implement OnContentChanged. This approach may not appeal to you.
If you want to detect changes in the child or children of controls without deriving from them, you can observe that such changes will affect the layout and so you can hook the LayoutUpdated event. The problem with this approach is that you need to keep track of the children that were previously added yourself by inspecting Child or Children looking for changes. You also have to be careful not to hang onto references to former children lest you create a memory leak. But it can be done.

Categories

Resources