简体   繁体   中英

programmatically add remove items from resource dictionary

I'm just new in wpf and am planning to use a listbox to generate recently opened or closed items. Everytime a file is opened, it gets added permanently added into a resource dictionary of type listbox item.

On the other hand, besides the listbox item is a button to remove the link.

Is this possible?

ResourceDictionary is a IDictionary . You can use the indexer to add/override items.

myResourceDictionary["somekey"] = someObject;

And you can use the Add() and Remove() method.

As for your scenario I would prefer to use an ObservableCollection instead and bind your listbox to it.

This is indeed possible and, in addition to bitbonk's answer, I would say this:

You probably don't want to have an ObservableCollection filled with ListBoxItems . ListBoxItem is a class that WPF uses for rendering and you'd not typically create these yourself. If you make your data source a collection of (for example) FileInfo objects, you can use data templates to make the display of those items in the list appear however you wish. This also has the benefit that you don't have to write code transform your data objects into a separate class just for display purposes.

(You'd likely use your own data/model type rather than FileInfo , but I don't know enough about your solution to suggest one.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM