私は現在、カスタム コード スニペットを保存およびコピーするための新しいツール ウィンドウを提供する C# のビジュアル スタジオ パッケージ プロジェクトに取り組んでいます。
これまでのところ、次のように小さなウィンドウを設計しました。
ウィンドウの XAML コードは次のとおりです。
<TreeView Name="tevTemplates" Background="#00000000">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<Image Margin="0,0,5,0" Source="{Binding Image}" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
そして、私が使用するカスタム データ モデル:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
namespace Sago.TemQWindow.Classes
{
/// <summary>
/// Represents a simple data item to display easy an item inside a view
/// </summary>
public class MenuNode
{
/// <summary>
/// Initializes a new instance of a Sago.TemQWindow.Classes.MenuNode object with the display title
/// </summary>
/// <param name="title"></param>
public MenuNode(string title) : this(title, null) { }
/// <summary>
/// Initializes a new instance of a Sago.TemQWindow.Classes.MenuNode object with the display title and image
/// </summary>
/// <param name="title"></param>
/// <param name="image"></param>
public MenuNode(string title, Image image)
{
this.Items = new ObservableCollection<MenuNode>();
Title = title;
Image = image;
}
/// <summary>
/// Gets or sets the display title
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets the display image
/// </summary>
public Image Image { get; set; }
/// <summary>
/// Gets or sets the sub items of the node
/// </summary>
public ObservableCollection<MenuNode> Items { get; set; }
}
}
ツール ウィンドウを開いた後、次のソース コードは、テンプレート フォルダーのコンテンツとサブ ディレクトリのコンテンツを読み込みます。
/// <summary>
/// Loads the whole folder structure of the given path recursive
/// </summary>
private void LoadStructure(MenuNode rootNode, string path)
{
// Gets all files
string[] files = IO.Directory.GetFiles(path);
foreach (string file in files)
{
// Creates and adds the sub node for all files inside the given folder
string clearName = IO.Path.GetFileNameWithoutExtension(file);
MenuNode node = new MenuNode(clearName);
rootNode.Items.Add(node);
}
// Gets all sub directories
string[] directories = IO.Directory.GetDirectories(path);
foreach (string directory in directories)
{
// Creates and adds the sub directory as a sub node
string clearName = IO.Path.GetFileNameWithoutExtension(directory);
MenuNode node = new MenuNode(clearName);
rootNode.Items.Add(node);
// Calls the method recursive
LoadStructure(node, directory);
}
}
次のステップでは、ツリービュー コントロール内のフォルダーとファイルを特定の画像で視覚化します。ご覧のとおり、データ モデルと XAML バインディング内にイメージ プロパティを既に実装しています。
問題は、これらの画像をプロジェクトに追加し、コードを介してそれらにアクセスする方法がわからないことです。私はすでにビジュアル スタジオ パッケージ プロジェクトを調査したので、Properties.Resources コンテンツにアクセスする可能性はありません。
誰かがこの問題で私を助けることができれば、私はとても感謝しています.