将图像添加到Framework Element Factory WPF时出现问题

时间:2009-12-08 19:01:16

标签: wpf image frameworkelement

我有一个WPF应用程序,它使用列表视图树进行导航。在这棵树中,我有一个对象类型列表,所以类似于:

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

右侧的每个对象名称旁边都有一个编辑图标。我想要做的是根据对象类型在对象名称的左侧添加一个图标。对象类型可以是单选按钮,组合框等。我有来自数据库的对象类型,所以没关系。我的问题是我有动态创建这棵树。所以我使用了FrameworkElementFactory,以便我可以在每个对象名称旁边添加一个堆栈面板,然后在每个对象名称旁边放置一个编辑按钮。希望这是有道理的。

到目前为止我尝试的是图像部分:

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;

当我尝试使用Bitmap方法时,我收到一个错误,我无法创建Window1的实例,这是我的WPF应用程序。我要做的就是确定对象类型是什么,然后根据它动态设置图标图像。我只是出于想法,有没有其他方法可以使用这种方法来实现这一目标?


只是一个更新,我终于通过了应用程序崩溃,更新的代码是这样的:

var 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());

var 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);

}

但是,图标现在不显示。谁知道为什么?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

BitmapImage bmpImg = new BitmapImage(new Uri("pack://application:,,,<AssemblyName>;component/<Path>/<ImageFilename>");

,其中

<AssemblyName> is the name of the assembly
<Path> is the path to the image within the project
<ImageFilename> is the name of the image with the extension

然后你可以这样做:

img.SetValue(Image.SourceProperty, bmpImg);