wpf combobox key/value dictionary

Go To StackoverFlow.com

0

I have a Combobox for choosing file extensions like "Images, (*.png, *.jpg)". I want to get the data from a Dictionary key/value pair. I have added string for the first part, for instance "Images" and for extensions I added a list since there can be more than one. I use this data in showing SaveFileDialog or OpenFileDialog. How can I use these extensions as a filter for SaveFileDialog? Could you please provide help?

ExtensionCollection = new Dictionary<string, IList<String>>();
ExtensionTypeCollecction = new List<String>();
Extensions = new List<IList<String>>();
perExtension = new List<String>();
perExtension.Add("*.png");
perExtension.Add("*.jpg");
Extensions.Add(perExtension);
ExtensionTypeCollecction.Add("Images");
ExtensionCollection.Add("Images", perExtension);

Thanks in advance

2012-04-05 21:54
by bilgestackoverflow


1

I think this is what you're after:

var d = new Dictionary<string, IList<string>>();
d.Add("Images", new List<string>{ "*.png", "*.jpg" });

var key = "Images";
var extensions = d["Images"];

var filter = key + "|" + string.Join(";", extensions.ToArray());

giving

filter = "Images|*.png;*.jpg"

or you can do this

var extString = string.Join(";", extensions.ToArray());
var filter = key +" (" + extString + ")|" + extString;

which results in

filter = "Images (*.png;*.jpg)|*.png;*.jpg"

and you can of course add the usual "|All files (*.*)|(*.*)" filter option if required.

2012-04-05 22:27
by Phil
Thanks for replying, I will tr - bilgestackoverflow 2012-04-05 22:30


0

Does this help?

   foreach (string ext in Dictionary["images"])
   {
       Debug.WriteLine(ext);
   }
2012-04-05 21:59
by paparazzo
Ads