2
我有一个使用列表视图树进行导航的WPF应用程序。在这棵树,我有对象类型的列表,所以类似这样:将图像添加到框架元素工厂时出现问题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;
当我尝试使用位图的做法,我感到我不能创建窗口1的实例,这是我的错误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);
}
但是,图标不显示现在。有人知道为什么
合并帐户到您注册一个用于注册(感谢,顺便说一句!)。请阅读[常见问题](http://stackoverflow.com/faq),我们喜欢在答案中回答问题和答案。 – Will 2011-02-28 18:53:19
另外,如果你有新的问题,不要害怕问一个新的问题。我不确定您的原创内容和您制作的内容是否相关。但我可以告诉你,你应该打开你的[WPF跟踪设置](http://i.imgur.com/mdi74.png)。另外,这里有一点protip ....'if(objectType == typeof(Combobox))'和WPF不是windows窗体。通过代码强制你的方式不是最好的方式。离得很远。所有这些代码都可以用一个简单的绑定来代替。 – Will 2011-02-28 18:57:28