C
C#17mo 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
mikernet17mo 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
Anton17mo 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
mikernet17mo 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
Anton17mo ago
I have an observable collection of ints/strings which the ItemsControl is bound to
mikernet
mikernet17mo ago
Etc
Anton
Anton17mo ago
the checkboxes are added by a totally different component so the templates in the item control should bot know about it
mikernet
mikernet17mo ago
Right, so why is binding to that going to be helpful for getting at the controls?
Anton
Anton17mo 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
mikernet17mo 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
Anton17mo ago
because the checkboxes are managed by a different component
mikernet
mikernet17mo ago
Idk what that means Why
Anton
Anton17mo ago
like a different module think of that module as imported from a dll also it is conditionally applied
mikernet
mikernet17mo 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
Anton17mo ago
yeah I could
mikernet
mikernet17mo ago
Ok, then why can't you wrap whatever you have inside the item template with something that overlays the checkboxes?
Anton
Anton17mo ago
the templates should not know about the checkboxes by design
Anton
Anton17mo ago
ig I kinda could
mikernet
mikernet17mo 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
Anton17mo 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
mikernet17mo ago
There's some good info here that you can use: http://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/
Accord
Accord17mo 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
More Posts
❔ Can someone explain asynchronous code?I've read the overview with the breakfast analogy on the Microsoft docs for .NET, and still don't ge❔ Styling ListView column header with one big gradientTrying to style a listview column header with a gradient. I want the gradient to be a seamless gradiPlease help me get started with proxyHello everyone, there was a need to write a program that will redirect outgoing requests from exampl❔ WPF not disposing of memory on window closeI'm developing a WPF app that mostly lives in the taskbar corner, but that can open a window to chan❔ Java => C#Hi all, Would anyone know enough Java to convert this codeblock from Java to C#? ChatGPT failed on ❔ Unpackaged WinUI 3 app raises 0xc000027b after publishI do have a problem and I would like to use your help. My app is not starting after publishing it wi❔ Creating WPF listHello, im a beginner in wpf and c# and im trying to make a ui that has this type of design where the❔ ✅ Unable to remove reference to projectplease forgive my frustration..... this really shouldn't be an issue..... but for whatever reason i'`System.String` doesn't have the `Substring` method or something is weird about this string??i've even tried this ```cs description.Length > 2043 ? description[0 .. 2043] : description;``` butVery very new to coding. - How do i make a celcius to Fahrenheit converter? (Its an assignment)Hey, i want to make a celcius to fahrenheit converter. This is the code i got, and i promise im bein