ナビゲーションにリストビューツリーを使用する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;
ビットマップアプローチを使用しようとすると、WPFアプリケーションであるWindow1のインスタンスを作成できないというエラーが表示されます。私がやろうとしているのは、オブジェクトタイプを特定し、それに基づいてアイコン画像を動的に設定することだけです。私はちょうどアイデアが足りませんが、このアプローチを使用してこれを達成する他の方法はありますか?
ただの更新で、私はついにアプリのクラッシュを乗り越えました。更新されたコードは次のとおりです。
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);
}
ただし、アイコンは現在表示されていません。誰もが理由を知っていますか?