C
C#2y ago
Anton

❔ WPF draw over other arbitrary controls

I want to draw over items contained in an ItemsControl, without wrapping the ItemsControl into any sort of other object. Can anyone suggest a starting point / best practice? With my knowledge of WPF I don't know where to start solving this one.
22 Replies
mikernet
mikernet2y ago
Adorners Overview - WPF .NET Framework
Learn about Windows Presentation Foundation Adorners, a special type of FrameworkElement that provides cues to a user, such as functional handles for elements.
Anton
AntonOP2y ago
I thought these had to wrap the element, but they seem to be more general. thanks Yeah actually it's not what I need. I actually realize I explained it wrong. I actually need to put actual controls over other controls What I need is to make checkboxes over other elements Specifically, the elements inside an ItemsControl But I can't modify the data template The other idea is if there's an event that triggers when an item is added to the ItemsControl, to inject the checkbox with that If I just bind to the underlying collection, then the event handler priority kicks in (I won't be able to modify the item before it's been created in the ui) I had to do a hack that: 1. defines a ui event service that binds itself to the same collection as the ItemsControl, but after it's been created, so the events are triggered after the individual items get created in the ItemsControl; 2. some INotifyCollectionChanged event handler logic that injects a checkbox into the newly created items, and then updates them when the underlying collection changes, but after the items are changed (supposedly). This kinda couples itself to the internals of ItemsControl, ig I could do something more generic by going through all of the items every time looking for a checkbox, perhaps a tagged one, adding one if none is found, and binding the sources correctly, but it seems as tho my event idea did not work out. https://github.com/AntonC9018/uni_csharp/blob/aa20cb73ac5ffe98636e0fbfbe91177a9c82f2db/sem2_lab1/SortingUI.cs#L139-L217 If you've got a better idea of how to do this, please enlighten me Keep in mind that I can't modify the templates in the ItemsContol That is, I can like wrap them in a panel like I did, but no checkboxes should be enabled there by default, and in general, the template shouldn't be bothered by that Nevermind, the problem is that ItemsContol does not parent the items directly, it may be wrapped in a border or something How do I get to the items?
mikernet
mikernet2y ago
What do you mean? I don't think INotifyCollectionChanged is what you are after here. The binding might not be (usually isn't) to a collection of controls, it's usually to a view model I need more info on the exact situation here to recommend something, i.e. how robust does this need to be for all possible ItemsControl configurations? Can you subclass ItemsControl and use that? Do you need to be able to support a variety of items panels? What if it's a virtualizing stack panel which recycles the item controls?
Anton
AntonOP2y ago
I have an observable collection of ints/strings which the ItemsControl is bound to
mikernet
mikernet2y ago
Etc
Anton
AntonOP2y ago
the checkboxes are added by a totally different component so the templates in the item control should bot know about it
mikernet
mikernet2y ago
Right, so why is binding to that going to be helpful for getting at the controls?
Anton
AntonOP2y ago
I looked for an event that triggers when some created control changed, or like the hierarchy changes, but couldn't find anything and had to try the aforementioned hack to try and imitate such event
mikernet
mikernet2y ago
Ok...let's start again. Why can the template not know about the checkboxes? Because the reason for that will dictate suitable approaches i.e. Why do you need to do it this way
Anton
AntonOP2y ago
because the checkboxes are managed by a different component
mikernet
mikernet2y ago
Idk what that means Why
Anton
AntonOP2y ago
like a different module think of that module as imported from a dll also it is conditionally applied
mikernet
mikernet2y ago
Conditionally applying is easy, you just show or hide it I still dont understand this architecture or why the template cant have the checkboxes It all sounds very strange Without an actual understanding of what you can or can't do it's hard to suggest something here Does it need to be an ItemsControl or can you subclass ItemsControl and use that?
Anton
AntonOP2y ago
yeah I could
mikernet
mikernet2y ago
Ok, then why can't you wrap whatever you have inside the item template with something that overlays the checkboxes?
Anton
AntonOP2y ago
the templates should not know about the checkboxes by design
Anton
AntonOP2y ago
ig I kinda could
mikernet
mikernet2y ago
You can modify the container it generates for each item So it puts the item into a container and overlays the checkbox on top
Anton
AntonOP2y ago
I'm gonna do it a bit more generically to allow introducing custom logic there but it should work thanks for an idea
mikernet
mikernet2y ago
There's some good info here that you can use: http://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server