This project is read-only.

Problem with IconSource and programmatically adding a tabpage

Dec 30, 2013 at 8:01 AM
Edited Dec 30, 2013 at 8:30 AM
I am pretty new to Wpf and very new to AvalonDock.
So far I like it and it helps me with the design I want.

Although I have searched this forum and, (might be my brain that don't work), I cannot just figure out how to set the IconSource when opening a UserControl programatically.

I'm not even sure that I open the UC the right way - I Would be greatful i someone could point me in the right direction.

This is how I do it right now:
private void mnuSettings_Click(object sender, RoutedEventArgs e)
 var documentPane = dockManager.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
   if (documentPane != null)
     LayoutDocument layoutDocument = new LayoutDocument { Title = "Settings"};
     UserControl ucSettings  = new MyNamespace.Usercontrols.ucSettings();
     layoutDocument.Content = ucSettings;
     layoutDocument.IsActive = true;
This works just the way I want it.
Now, if I try to add:
layoutDocument.IconSource = "path_to_image";
I get the following error: Cannot convert string to System.Windows.Media.ImageSource
Fine, I'll get that - so I tried:
var imgIcon = (ImageSource)new ImageSourceConverter().ConvertFromString("path_to_image");
layoutDocument.IconSource = imgIcon;
Now the designer looks ok, all work until I try to open the control and get the 'object reference not set to an instance of an object' error.

Any help would be great.
Thanks in advance.

I Have found a solution, but I still don't know if this is the best one.
As before, if someone has a suggestion on changes in my code I would be greatful.
What I did was:
BitmapImage imgIcon = new BitmapImage();
imgIcon.UriSource = new Uri("pack://application:,,,path_to_image");
and just added:
layoutDocument.IconSource = imgIcon; 
It is now working fine - but just for the fun of it and to make it little better down the road I made a function for it to avoid a lot of code:
private BitmapImage getImage(string image)
  BitmapImage imgIcon = new BitmapImage();
  imgIcon.UriSource = new Uri("pack://application:,,," + image);
  return imgIcon;
So now I just call:
layoutDocument.IconSource = getImage("path_to_image");
I Hope I can help someone else with this.
Marked as answer by DanielWiberg on 12/30/2013 at 4:44 AM