如何将XamDataTree图标设置为位图而不是文件名

本文关键字:位图 文件名 设置 XamDataTree 图标 | 更新日期: 2023-09-27 17:52:47

我已经设置了XamDataTree图像,但我不想每次都将图像文件夹复制到我的调试文件夹。相反,我想将图像添加到我的项目资源中,并从那里使用它。目前没有图像显示,因为它需要一个路径到一个图像,我给它一个实际的位图。TreeNode中的Icon属性是在代码的另一部分设置的。

这是我的Xaml代码:
<ig:XamDataTree
    Grid.Row="1"
    Name="MyTree" 
    ScrollViewer.CanContentScroll="True"
    ItemsSource="{Binding ComparedContents}"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    >
    <ig:XamDataTree.CheckBoxSettings>
        <ig:CheckBoxSettings CheckBoxVisibility="Visible" />
    </ig:XamDataTree.CheckBoxSettings>
    <ig:XamDataTree.CollapsedIconTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=Icon}"/>
        </DataTemplate>
    </ig:XamDataTree.CollapsedIconTemplate>
    <ig:XamDataTree.ExpandedIconTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=Icon}"/>
        </DataTemplate>
    </ig:XamDataTree.ExpandedIconTemplate>
    <ig:XamDataTree.GlobalNodeLayouts>
        <ig:NodeLayout
            Key="Children"
            DisplayMemberPath="Text"
            TargetTypeName="Model.TreeNode" 
            >
        </ig:NodeLayout>
    </ig:XamDataTree.GlobalNodeLayouts>
</ig:XamDataTree>

这是我的模型,每个属性都有一个值的私有存储,如果它发生变化,就会触发一个事件。

public class TreeNode : INotifyPropertyChanged
{
    public Label Text;
    public System.Drawing.Image Icon;
    public ObservableCollection<TreeNode> Children;
}

如何将XamDataTree图标设置为位图而不是文件名

假设您将图像嵌入到应用程序中,您可以使用包uri将图像加载到节点模板中。不要在Icon属性上使用Image类型,你应该使用Uri并将其设置为如下内容:

Icon = new Uri("pack://application:,,,/Resources/Images/icon.png");

你需要稍微修改一下模板中的绑定,因为这个模板的DataContext将是一个XamDataTreeNodeDataContext。这个对象有一个Data属性,它将是你的TreeNode对象。你的绑定应该更新为:

<Image Source="{Binding Path=Data.Icon}"/>