Silveright UIElementCollection change notifications? - silverlight

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.

Related

Custom FrameworkElement design

I'm creating a component that looks a bit like a web:
I created a custom FrameworkElement named Web containing a VisualCollection to draw the component. However the design is currently really poor and I want to make it again from ground up.
But I can't figure out how I should design the quarters of this web.
Should they be custom FrameworkElement too? Knowing that I need to handle "click" event on them.
Or should I just make an object extending DrawingVisual per Quarter and make a hittest to know when a quarter has been clicked?
It's my first custom element and I'm a bit lost.
Thank you
You can create a class inherited from Panel. And you can arrange you children elements in a a circular fashion as explained here using Measure and Arrange method. Create another control, which should be clickable. Add those controls as a Children to your custom Panel. Template your container control, so that it can show Arc using ArcSegment.

WPF loading grid child based on treeview click

I'm developing a WPF C# app where I have a tree view control and if the user clicks on a node in the tree, a node-specific detail 'form' appears in a named Grid somewhere else in the form. If you click on a different node in the tree, the displayed detail form checks if the contents are saved, is dismissed, and a new detail form appears in it's place.
What I need is some starting advice. Can I still implement the forms as standalone xaml, then put some some of 'container' in the grid that I throw the form into as a child? Or just add the form as a grid child somehow. How do I programmatically load the form I want in the grid and communicate with it?
Thanks for any assistance!
Corey.
Use an event aggregator design pattern, see here for details:
http://martinfowler.com/eaaDev/EventAggregator.html
You can then have some other code which listens for the node change clicks via the event aggregator and response accordingly. This will decouple your code and make it more testable.
Am assuming you are using mvvm?
If not read up on it - will make it easier.
then you have your form with the treeview on it, bound to its itemsource on the view model.
Usually an items control like a treeview will have a selecteditem property on it.
Bind that to a property on your viewmodel which is of the type of objects contained in your treeview. Call this for example CurrentlySelectedItem.
Your details 'form' can be a control or whatever you want on the same form.
Now depending on how complete your object is - you have at least two options. If your object in the treeview has all the data you need already, then just bind the details to CurrentlySelectedItem.
Obviously it must implement INotifyPropertyChanged to tell the binding system to update the values.
If the object doesn't have enough info, then on the setter of CurrentlySelectedItem you can then fire a method to load the full object and then bind the details to that full object.
Alternativley, another popular approach, you could have the details form as a self contained control that subscribes to a message and when it recieves the message with the key of the treeview object, it loads the required info.

Bind List of UControls to a Controls Children property?

I have got an ObservableCollection<Button> buttonList; (this can be any other control: TextBox, TextBlock or even a UserControl).
I want to bind this List via DataBinding to a parent control (lets say a Grid or a WrapPanel,...)
Is this somehow possible? Children property is read only.
I do not want to do this in the program code -> Dont iterate through the list and add every item to the children property.
I want to BIND the List to a parent control -> the View is automatically updated, every time I add a new Item to the List.
Any Ideas?
Use ItemsControl, and bind your collection to ItemsSource.
And don't forget to read Dr. WPF's articles: ItemsControl: A to Z. This is second time I'm mentioning this blog today here, but he really-really deserves to be mentioned in every post related to collection of items and bindings :).
Cheers, Anvaka.

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.

Silverlight Panel OnChildAdded event?

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.

Resources