creating custom contentcontrols which support hierarchy - c#

I'm creating some custom object using ContentControl and by adding them to toolbox allowing user to drag/drop them on a page. Now, I'm facing a situation where some of these controls have children. In case it is only one item without children I retrieve each object's id in Tag property, however, for the 2nd category this won't be the case, since I need to store and retrieve a hierarchy set of IDs. After few searches I ran into MSDN forum and it is almost close to what I need to do but I was wondering there are better and more optimized methods of implementation.
Any help will be appreciated.
Thanks.

If an object can have more than one child you use an ItemsControl and not a ContentControl. And an ItemsControl containing ItemsControls is a hierarchy.

Related

How can I use a User Control as a Diagram Node in XamDiagram?

Working with XamDiagram in the Infragistics WPF controls library, I'm looking for a way to use existing user-controls as Diagram Nodes instead of the out-of-the-box rectangles/circles/basic shapes.
If I inherit from DiagramNode, I can set the DisplayTemplate and EditTemplate properties, but would need to provide a type of System.Windows.DataTemplate. Since the user controls I want to use are fairly complex and pre-existing, I'd like to avoid creating a brand-new DataTemplate for it.
How can I use a user control as a DiagramNode?
Both the DiagramNode and DiagramConnection have DisplayTemplate and EditTemplates you can use. It is actually all very well documented here and for more questions you can check this out.

Customizing the FrameworkElements used to display the contents of an ObservableCollection

So it seems like what I want to do should be straightforward, but I haven't been able to find a way to do it...
I need to display a list of objects that represent custom elements for entering data. Exactly how each object is displayed depends on the parameters of the object - so it could be a grid containing a name, description, and text box. It could be a grid with a couple labels and a dropdown. It could be an expander that contains multiple sub-objects. It could be something new that hasn't been built yet (so it needs to be extensible). Right now, I am populating this list by creating the FrameworkElement for each object and then adding it to a Grid by hand.
I would like to switch to keeping my objects in an ObservableCollection, and then binding that collection to a ListBox (or similar). That way, when new objects are added or removed from the list, the UI would automatically update itself accordingly. What I can't figure out is, is there a way to point it to my C# method for creating the custom-configured FrameworkElement for each object, so that when new objects are added the appropriate elements will be added to the UI?
Well, you're on the right track as far as wanting to use an ObservableCollection<T> and a ListBox control. Though, I'd venture to say you might want to simply use an ItemsControl since you probably don't care about selecting a particular item, but merely displaying an enumeration of items, and the ListBox would allow you to actually select one of those items.
Your problem is that you want each item in the list to display differently depending on certain criteria. For this you'll want to look at the DataTemplate and DataTemplateSelector classes.
Basically, a DataTemplate is a way of saying "I want my item to look like this.", and a DataTemplateSelector is a way of saying "I want to select this specific DataTemplate based on this criteria."
Here are a few examples on how to use the DataTemplate/DataTemplateSelector classes:
http://tech.pro/tutorial/807/wpf-tutorial-how-to-use-a-datatemplateselector
http://mikestedman.blogspot.com/2009/04/datatemplateselector.html
http://wpftutorial.net/DataTemplates.html
Seperating the presentation from the model is always a good idea. It seems like you are on the right track.
Foreach object type, you should create a DataTemplate and then use ItemTemplateSelector to select the correct template for each object type.
Good luck

Custom items in ListView control

I need to add a custom item to ListView control (like on the picture), is it possible ? And if it is, what's the best way to do it ?
I don't know that this is possible with Winforms. The list items in a System.Windows.Forms.ListView are System.Windows.Forms.ListViewItem objects contained in a strongly typed collection.
You could try to create a subclass of ListViewItem, but since that class inherits directly from System.Object and is not an actual windows form control, you might be borrowing trouble, as you would need to replicate all the functionality of the inheritance chain of an actual control.
Now, if you are not too far in to the project, you might consider looking at switching to WPF. The ListView in WPF uses controls as items, so you could easily create a usercontrol that you would use as your list items.
You might be able to find a control library with a control that gives you the functionality you want, but for the most part, the good libraries are commercial and can be prohibitively expensive for small shops and individual .
I did a quick google search for any library that offers this capability, but I could not find one that displayed custom controls.
Not sure if this is what you're after, but ListViewItems have a Tag property that can store custom data about each item.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.tag.aspx
How about creating a user control. In that case you can set your user controls at the top and listview below. Or are you trying to add these controls as items in the listview itself?

ASP.net: More than 2 Nested Repeaters

My data comes from a database. I have an item data table with some basic properties.
Item
{
ID,
Name,
Description,
...
}
Then I have a many to many relationship table with:
Parent
{
ParentID,
ChildID
}
I'm iterating through each item and displaying its children; and its children's children, etc. I assume this would best be accomplished with nested repeaters, but I could be wrong.
How do I get multiple heirarchical levels using asp:repeaters? I've only ever used one nested repeater, I have no idea how to do 3+.
Personally I probably do this by creating a custom control with an Item property and some sort of either Parents or Children property. The control would display details on Item, and then use a repeater to show each element in Parents / Children, where for each item the repeater recursively uses the same control to render the item.
I would do what #Kragen said. But if you really think that creating two components is too much you should use one repeater and two foreach loops in on data bound.
Using two repeaters one inside of another is overcomplicated not to mention 3 repeaters :).
A Parent-Child relationship, which is many-to-many? This doesn't make sense, at least calling them a parent and a child doesn't make sense. If it is really many-to-many, it's a network, if a child only has ONE parent, then it is a hierarchy.
With the former, I'm not sure visualizing them in that way is any good. How do you visualize a child with more than one parent?
With the latter, why not use a TreeView with a custom item template? It handles all the hierarchy stuff automatically. The problem with repeaters is that if you are not doing them dynamically (creating them in code), then the level of nesting is fixed and you cannot go as much as you want. Doing them dynamically would work, but would bring overhead.
I'm not very sure what exactly you are trying to accomplish. Maybe providing some more details about the end-result you need to see would be helpful.

Windows Form: showing child object value in the DataGridView

I have a productVariant object which has child product object. I want to show the value in the DataGridView, can anyone let me know how to do this?
Here is the structure of the object.
I tried to bind "ProductVariant.Product.Name" to the DataProptertyName in the DataGridView, however, it did not not showing any value.
Can anyone help with this? Many thanks.
DataGridView-controls can not bind child objects automatically.
If you are working with Win-forms, my suggestion would be to create two Dialog windows.
One is for parent-objects(with a DGV) and another is for child-objects(with a DGV).
When the user opens the parent window, parent objects would be loaded row-wise in the DGV.
Then if the user double-clicks a row on the parent-window, the child-window would be shown up and the child objects of that parent would be populated on the DGV on the child window.
In this way you can keep things clean no matter how deep the parent-child relationships go. Moreover, add, edit, delete, etc. operations would be much cleaner.
You can also use some third-party controls like, HierarchicalDataGridView.
WinForms binding does not allow traversal of nested properties, though such a thing is possible in WPF. The easiest solution for WinForms is to add a property at the top level (the actual type being bound) that represents what you want to bind--ugly, I know.
You could implement a custom TypeDescriptor and provide a PropertyDescriptor that does the same thing. This is the cleanest solution as far as what gets exposed outside of the class, but it's a non-trivial amount of work.

Categories