1

我已经设置了 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;
}
4

1 回答 1

2

Assuming you embedded the image into your application, you can use a pack uri to load the image into the node template. Instead of using an Image type on your Icon property you should use Uri and set it equal to something like this:

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

You need to modify the binding in your templates a bit because the DataContext of this template is going to be a XamDataTreeNodeDataContext. This object has a Data property which will be your TreeNode object. Your binding should be updated to:

<Image Source="{Binding Path=Data.Icon}"/>
于 2013-01-15T22:02:34.370 回答