Add event to dynamically created children of UIElement like Canvas - c#

I have a Canvas with different Elements which are created and removed dynamically at runtime inside a canvas, which itself is in a custom Usercontrol.
Now I want to add a few Touchevents, like ManipulationStarted. However, if I bind it on the Canvas, I get the canvas back as sender in my event. I need, however, the specific UIElement that was touched, so I guess I need to bind the event directly to the children. How can I achieve this?
PS: I've already tried creating a custom Event inside the Usercontrol, which is added to the specific child when it's created, however the performance seems to suffer immensely from this approach.

Related

Contentcontrol or dockpanel for navigation wpf

I use a Contentcontrol to show the user controls of the program, Now there is a problem for me to close the user controls After searching, I found an example that The user controls is loaded on a DockPanel
Now my questions:
What is the difference between these two controls? (Dockpanel vs ContentControl)
Is it okay to use this control(dockpanel) Instead of Contentcontrol to display application user controls?
Is there a similar code for Contentcontrol?
ucChild ChildWindow = new ucChild();
ChildWindow.ParentControl = this.UIPanel;
UIPanel.Children.Clear();
UIPanel.Children.Add(ChildWindow);
Standard disclaimer for people coding WPF like it is WinForms: First off; direct UI manipulation like this is a bad idea. You should
be modifying a view model and allowing the binding system to update
the UI. Use the MVVM pattern; WPF will work for you instead of
against you
To your actual questions:
Everything. I mean; they both inherit from FrameworkElement but that's about it in terms of commonality.
A DockPanel is as the name suggests, a Panel. That is; it controls the layout and sizing of one or more child elements. Specifically, DockPanel is good at situations like the following: you want an element to use up a full column of width, then another span the top (except for the previous element) and have the last element fill the remaining space.
A ContentControl is basically a placeholder, its purpose is to expose a settable (and most importantly, bindable) Content property that you can stuff another control into. Even better; you can put an actual object there and use a DataTemplate to control the display (this approach would conform to MVVM).
You can't really replace one with the other, see above
No. ContentControl is not a Panel and so does not have the Children property.

How to disable the capture of a Click event

Using VS2010 on .NET v4 I have the following problems.
I have created two custom Controls, Cell and Board and I want to use the Cell control to capture a number of events and to display some data. The most important event I need to capture is the Click event.
When the custom control has no child control items this is not a problem.
At the level wher Cell is used I can bind an event handler to the Click event with no problem.
public partial class Cell : UserControl
{
....
}
public partial class Board : UserControl
{
private void InitializeComponent()
{
...
this.Cell99.Click += new System.EventHandler(this.Cell_Click);
...
}
}
The Cell control is rather small (about 50x50 pixels in size) and one of the things I want it to display is an integer value as big as possible (about 50 x 50 pixes in size ;-) No problem in doing that, I want to use a Label control for that.
As soon as I add another (e.g. Label) control to Cell it no longer can see some events because the child event is getting 'first rights' on it. For the pixels that are covered by the Label control it will capture the Click event that I want to handle with the Cell control and not with the Label contol.
If it was only one control that I want to use in Cell the solution would be to link the event from the Label control to the Cell control. But I have at least 10 different (Label) controls that need to go in Cell to display all the information I have. Forwarding all the events for all the controls to its parent looks like a bit of a hassle to me. (Except from being prone to errors and other bugs.)
What is the propper way of disabling event capturing for child controls in a user control?
As said I have thought of forwarding events from child to parent but rejected that for now.
Then I have thought of not using controls to display the data but generate a bitmap with the information in it and use that as a background. I'm... not looking forward to that!
And finaly I came with an idea of putting one extra control, on top of all the others, and forward the events it's captures to the parent. That might be the simplest way to continue.
Somehow I thing there must be a better way to approach this problem but how?
Any advise is welcome.
Implement a label class which inherits from Windows.Forms.Label. In this derived label/component class add a method or mechanism to forward any click event received by the label to the parent Cell control. Any labels which are added as child controls of a Cell would be of this custom label class and would all forward their Click events to the parent without having to manually do so for each label instance.

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.

Updating a control from another control child

I have some problems trying to update components of surface elements. I dont know if my approach to the problem is wrong, since I'm new to the topic.
My point is represented by the following diagram
According to the option that is selected in the menu, load different user controls as only child of StackPanel but i'm habing problems for update the Listview from loaded user controls, example: when I save a new item I need to recharge the list of items in the ListView
MVVM would be a good pattern here. If you have a problem passing data between controls - why not introduce them on top of unified data layer? Consider this:
Three radio buttons in your Menu, each one's IsChecked property bound to Visibility property of your respective UserControl.
StackPanel holding all three UserControls
ListView bound to ViewModel's List<Item>
Each of your UserControls bound to ListView.SelectedItem: one of them using TextBlock for read-only, one using TextBox for editing. Third one would create new item in your List<Item>. You would have to create ItemTemplate for each or create one UserControl (since they look very much alike) and use DataTemplateSelector.
If you're not familiar with MVVM here is a good start. You can also use one of the existing frameworks like MVVM Light
You can create an event on your child
public delegate void HandleNAMEOFYOURHANDLEEVENT();
on your child class
public event HandleNAMEOFYOURHANDLEEVENT yourInstance;
to use it on your child class
if (!ReferenceEquals(yourInstance, null))
{
yourInstance();
}
and you declare it on your parent like other event.

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