tags:

views:

273

answers:

1

I have a WPF application that uses a list view tree for navigation. In this tree, I have a listing of object types, so something similar to this:

+Domain
-Process
  -Procedure
    -Task
      -Object Name - Types

Next to each object name on the right side is an edit icon. What I'd like to do is add an icon to the left of the object name based on the object type. Object types can be radio button, combo box, etc. I have the object type coming from a database, so that's fine. My problem is I have this tree being created dynamically. So I've used the FrameworkElementFactory so that I could add a stack panel next to each object name and then put an edit button next to each object name. Hopefully that makes sense.

What I've tried so far is for the images part:

DataTemplate dataTemp = new DataTemplate();

//create stack panel and text block

FrameworkElementFactory stkPanel = new FrameworkElementFactory(typeof(StackPanel));

FrameworkElementFactory txtBlock = new FrameworkElementFactory(typeof(TextBlock));

FrameworkElementFactory img = new FrameworkElementFactory(typeof(Image));

Image imgIcon = new Image();

BitmapImage bi = new BitmapImage();

//set value of text block to the object name of the tree

txtBlock.SetValue(TextBlock.TextProperty, taskObjectRow["ObjectName"].ToString());

string objectType;

objectType = taskObjectRow["Type"].ToString();

if (objectType.ToString() == "RadioGroup")
{

    bi.UriSource = new Uri("Radiobutton.ico");

    imgIcon.Source = bi;

    img.SetValue(Image.SourceProperty,imgIcon);

}

////set the orientation of text in the stake panel to horizontal

stkPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
                                        FrameworkElementFactory editTreeBtn =new 

FrameworkElementFactory(typeof(editTreeButton));


editTreeBtn.SetValue(editTreeButton.ObjectIDProperty,taskObjectRow["ObjectID"]);

//append txt block and user control to the stack panel

stkPanel.AppendChild(txtBlock);

stkPanel.AppendChild(img);

stkPanel.AppendChild(editTreeBtn);

//add stkPanel to data template

dataTemp.VisualTree = stkPanel;

When I try using the Bitmap approach, I get an error that I can't create an instance of Window1, which is my WPF application. All I'm trying to do is determine what the object type is, and then set an icon image dynamically based on that. I'm just about out of ideas, is there any other way to accomplish this using this approach?

Thanks,

A: 

Just an update, I finally got past the app crashing, which the updated code is this:

FrameworkElementFactory img = new FrameworkElementFactory(typeof(Image));

BitmapImage bi = new BitmapImage();

//set value of text block to the object name of the tree

txtBlock.SetValue(TextBlock.TextProperty, taskObjectRow["ObjectName"].ToString());

string objectType;

objectType = taskObjectRow["Type"].ToString();

if (objectType.ToString() == "Combobox")

{

bi.BeginInit();

bi.UriSource = new Uri(@"Combobox.ico", UriKind.Relative);

bi.EndInit();

//Uri uri = new Uri("Combobox.ico", UriKind.Relative);

//Image imgTest = new Image();

//ImageSource imgSource = new BitmapImage(uri);

//imgTest.Source = imgSource;

img.SetValue(Image.SourceProperty, bi);

img.SetValue(Image.VisibilityProperty, Visibility.Visible);

} However, the icon doesn't show now. Anyone know why?

Ryan