control object subscribing to event from mainwindow - wpf

My application is a UserControl which has a grid whose content is loaded dynamically. Essentially, I maintain a list of child objects and replace the grid child whenever needed. I now need the child object to subscribe to an event that is raised by the UserControl. If it was the other way round I would have just done child.property +=.... How do I get this to happen?

If the child object already implements the features that you need to trigger, you can have the UserControl subscribe to the event on its own, then call the child functionality from the handler.

Related

Dynamically binding to property of child controls

Would it be possible to bind to Validation.HasErrors on any child control of a panel, without explicitly binding to every named control? Specifically i want to fire a trigger on a general style of a panel if any child control is in error state.
Thanks.
I would use an attached behavior that uses the LogicalTreeHelper (or VisualTreeHelper as a backup) to subscribe to the dependency property changed event of Validation.HasErrors for each child element via DependencyPropertyDescriptor. From there you would just update your own attached property (say, CompositeValidation.HasErrors) and bind to that.

pass message from the parent window to child user control wpf

I have a window that is hosting a user control. I wish to pass (raise) events in the parent window that should reach the user control.
How do I define events in the parent window that the child user control can also receive.
As I recall the steps should be:
Define a delegate in your parent window
Define an event in your parent window
Lets say in your window Loaded event attach the usercontrol to the
event, eg -
this.MyEvent += new
this.MyDelegate(this.UserControl1.SomeMethod);
Take note that
SomeMethod must match the MyDelegate definition.
Raise the event in your parent window per application logic.
For examples of how to define delegates/events then google is your friend ;) Also don't forget to unregister your usercontrol from the event, when it's no longer required (when the window closes).

WPF and events from dynamically created controls

I need some help to implement a common behavior in some controls.
In my WPF application, I have a main form that contains a panel and a button:
Ok
The button will run a Save method when clicked.The Save method reads some data from the form and saves the data to a database.
The panel is populated with dynamically created controls (such as textbox, dropdownlists, etc). The main form instantiates a MainViewModel class. This MainViewModel class instantiates a class called UIFactory. So we have 3 levels here.
In the UIFactory class the controls is being created. The Panel from the main form is sent as a parameter to a method in the MainModelView class called GenerateUI. This GenerateUI method in the MainViewModel class calls a GenerateControls method on the UIFactory class that takes the same panel as a parameter. The GenerateControls method in the UIFactory class then adds dynamically created controls on the panel.
What I want to achieve is that whenever the user hits ENTER when he is typing in one of those dynamically created controls e.g a textbox, I want that behavior to be the same as clicking on the button in my main form. But how do I do that? I thought of implementing Routed events on my controls, but I can't figure out how to do it. Could you please advise me on how to achieve my goal?
Best Regards,
OKB
Maybe the Keyboard.KeyUp attached event might help you. You could set it on the main panel which contains the dynamically created controls and then perform the save operation if the pressed key was the ENTER key.
I did manage to create a work aroind to my problem:
What I did was to create a custom user control(let's call it a container). This control is hosted in my wpf application using the WindowsFormsHost instead of the panel. Then I add the dynamically created user control to my new custom user control (the container) and add a KeyEventHandler on each child control's KeyUp event.
I created a custom event and event handler in my container that will catch all KeyUp event from the child controls, check if the e.KeyValue == 13 (ENTER) and then raise my custom event from the container that will be handled in my wpf form. Ugly as h*ll, but it works.

Different data contexts in a SL3 form

I wrote a custom Silverlight 3 control that uses a class as its data context (MVVM pattern). I want to place this control on another control (form) through XAML. The child control exposes a Dependency Property that when set through XAML, will make it show detailed info.
So an example is that the child control shows order details data, and I want to place it on a form that show user orders. When you select an order, the selected item value on the parent control (orders list), is data bound to the child control, to show details.
The problem is that the child control’s dependency property's OnChanged handler never gets called. If I do not set a data context on the child (so it uses the parent's data context) all works fine, but when I set a different data context, it breaks down.
Ideally, your ViewModel would be for the outer UserControl and a property on the ViewModel will be the DataContext of the Inner/Child userControl
Its true that when the Parent control's DataContext is set, it is propogated down to all its child controls. But the child control has an option of overriding this behavior by setting its own DataContext (which you seem to be doing in your example). Hence by the rule of preferences, the child control's DataContext is given more preference and thus it overrides the parent's one. Also since the child's DataContext never changes after it's initially set, the DP never gets invoked.
So I thought about this some more, and I understand what is happening, but I think its very confusing, and is not done right. If I am doing data binding on a control in the main page, it should use the context of that page to do the binding. And binding I do inside the control should use the control's context.
The way it works now uses the control's context no matter where I put the binding expression (unless I am doing E2E binding, then its using the main page's context). That is silly to me. But at least I understand it now.
I solved the problem using Element to Element binding, and got it to work. I hope the SL team would change this behavior.

Silveright UIElementCollection change notifications?

I am deriving a class from the Silverlight Panel class so that I can perform some custom positioning of the child elements of the panel. How do you find out when the collection of child items has been changed? The Panel.Children collection does not have any events indicating change notifications.
Do I have to scan the Children collection each time a measure occurs and look for the elements that have been added and disappeared?
Try this post: Silverlight: Getting notified upon changes to a Panel’s Children (of type UIElementCollection) property. Not a perfect solution, but might help.

Resources