Silverlight Panel OnChildAdded event? - silverlight

I'm trying to derive from the Silverlight Panel control to add some custom logic whenever a control is added to the Panel. I can't seem to find an "OnChildAdded" event (Or something similar) on the Children collection. Does anyone have any suggestions for how I can tell when a child control is added to a Panel or do I have to write my own container control?
Thanks,
Paul

Asked before here - unfortunately you can't get notification with the existing UIElementCollection Children property, but you could try a custom implementation.

Related

How to make custom WPF ContentControl that supports drag-and-drop at design time?

I want to create custom WPF control that has a single "child" control inside. Subclassing ContentControl or UserControl works, but has one flaw: these controls don't work in designer mode.
By "don't work" I mean this scenario: suppose I have a Canvas with my custom control in it. I want to put, say, a Button inside my control. I drag it from the toolbox, and it appears inside my control. However, XAML view shows that the new button actually belongs to Canvas, not to my control.
I can place it inside my control by manually editing XAML, but I want the designer to work too.
Interestingly, when I subclass Canvas, Grid or Panel, designer works as expected. However, these controls have many children, which is not what I need.
How can I make a single-child control that works in designer?
how about inheriting from Border? that way you could spare yourself the hassle with Designer Extensibility
I had the same problem with a content control I am writing and found an easy solution on this
StackOverflow thread.
Just implement the HitTestCore method:
protected override System.Windows.Media.HitTestResult HitTestCore(System.Windows.Media.PointHitTestParameters hitTestParameters)
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
I also had a similar question here.
But after digging and digging it seams that the real answer is "NO", there isn't any official way to support dragging controls straight into a custom Content-Control at Design-Time, even implementing 'HitTestCore' as Stephan's answer suggests, does not enable drag&drop at design-time for ContentControl.
For such purposes you should consider inheriting from either Grid or Panel (to allow multiple child controls), or Border (to allow single child).

Pass a value from child to parent in Silverlight

I am working on Silverlight project.
I added a custom user control (Say, control1) which has a text box and button to a xaml page (Say, Page1).
Now what I want to do is when users clicks on the button, i want to pass the value in the textbox to Page1 and do something.
So basically, I am looking for a way to pass back a value from child to parent page in Silverlight.
Thank you.
You should look into the Model View ViewModel (MVVM) pattern. It works very well with WPF and Silverlight.
http://en.wikipedia.org/wiki/Model_View_ViewModel
http://karlshifflett.wordpress.com/mvvm/ (lots of good information and demos)
You can do this through binding. Bind the Text value of the TextBox to a string property in your ViewModel and use that property throughout the code.
All the controls within your user control are accessible within your main page. If possible, write the click event of the button within the main page and you'll be able to access any control's property. Hope that work for you.

WPF - TextBlock - Cannot override OnRender

I am creating a custom control by deriving TextBlock, my intention is to do some custom rendering based on some dependency properties. However the OnRender method is sealed on TextBlock. Although I can get my work done by overriding OnRenderSizeChanged, this is not correct. Any ideas on how can i do it the right way?
Thanks in advance.
In WPF you normally work with styles or Control templates to change the appearance of a control. You can download templates for all controls from Microsoft and play around with them.
Read more here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate(v=VS.100).aspx

Wpf custom control

I have a customn control which display a window with another custom control inside. The developper can redefine the control template, it works but I don't find a way to allow developper to redefine the template of control inside the window that the custom control display...
Ok, I found. The solution is simply to set the other control as a dependency property inside the first (parent) control...

WPF: Custom Window

There are many articles that described how to create a custom shaped window in WPF but none of them (at least I can't find any) described how can build a reusable one such as inheriting from it in other windows.
I have tried to create a ControlTemplate from my custom window. The problem is with the Close button and the MoveDrag() method.
If I wire the event to one of my other controls in ControlTemplate their Parent property is null, so I can not drag or close the window.
Does anyone have any ideas how can I create a reusable custom window template or something?
Unfortunately there is no such things as visual inheritance in WPF. (no xaml inheritance to be more specific)
For your specific issue, instead of inheriting, you could create a CustomForm as a template (with a big empty container in the middle), and then create all your other forms as usercontrols that fill that container.
The following will return the window object containing the control:
Window.GetWindow(myControl)

Resources