在RadTreeListView中显示图像

时间:2014-12-10 21:46:59

标签: silverlight telerik radtreelist

我有一个RadTreeListView,我想在没有子节点的行上显示图像。剩余节点有子节点,因此它们显示为“展开/折叠”符号。我读了HierarchicalDataTemplates,但不知道如何在RadTreeListView上使用它。有什么建议吗?

XAML:

<UserControl x:Class="ExpandCollapseImage.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerikSdk="http://schemas.telerik.com/2008/xaml/presentation"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <telerikSdk:RadTreeListView x:Name="radTreeListView" AutoGenerateColumns="False" >      
            <telerikSdk:RadTreeListView.ChildTableDefinitions>
                <telerikSdk:TreeListViewTableDefinition ItemsSource="{Binding Items}"  />
            </telerikSdk:RadTreeListView.ChildTableDefinitions>            
            <telerikSdk:RadTreeListView.Columns>
                <telerikSdk:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />
                <telerikSdk:GridViewDataColumn DataMemberBinding="{Binding Count}" Header="Count" />
            </telerikSdk:RadTreeListView.Columns> 
        </telerikSdk:RadTreeListView>
    </Grid>

</UserControl>

XAML.CS:

using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace ExpandCollapseImage
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            BindData();
        }

        public class WarehouseItem
        {
            public WarehouseItem(string name, int count, string imageUri)
            {
                this.Name = name;
                this.Count = count;
                this.Items = new ObservableCollection<WarehouseItem>();
                this.Photo = imageUri;
            }
            public string Name { get; set; }
            public ObservableCollection<WarehouseItem> Items { get; set; }
            public int Count { get; set; }
            public string Photo { get; set; }
        }

        public class WarehouseService
        {
            public static ObservableCollection<WarehouseItem> GetWarehouseData()
            {
                //System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                ////img.Cursor = Cursors.Hand;
                //img.Source = new BitmapImage(new Uri("/Images/Add.png", UriKind.RelativeOrAbsolute));

                ObservableCollection<WarehouseItem> data = new ObservableCollection<WarehouseItem>();

                WarehouseItem drinks = new WarehouseItem("Drinks", 35, string.Empty);
                drinks.Items.Add(new WarehouseItem("Water", 10, string.Empty));

                WarehouseItem tea = new WarehouseItem("Tea", 20, string.Empty);
                tea.Items.Add(new WarehouseItem("Black", 10, string.Empty));
                tea.Items.Add(new WarehouseItem("Green", 10, string.Empty));
                drinks.Items.Add(tea);
                drinks.Items.Add(new WarehouseItem("Coffee", 5, string.Empty));
                data.Add(drinks);

                WarehouseItem vegetables = new WarehouseItem("Vegeatbles", 75, string.Empty);
                vegetables.Items.Add(new WarehouseItem("Tomato", 40, string.Empty));
                vegetables.Items.Add(new WarehouseItem("Carrot", 25, string.Empty));
                vegetables.Items.Add(new WarehouseItem("Onion", 10, string.Empty));
                data.Add(vegetables);

                WarehouseItem fruits = new WarehouseItem("Fruits", 55, string.Empty);
                fruits.Items.Add(new WarehouseItem("Cherry", 30, string.Empty));
                fruits.Items.Add(new WarehouseItem("Apple", 20, string.Empty));
                fruits.Items.Add(new WarehouseItem("Melon", 5, string.Empty));
                data.Add(fruits);

                WarehouseItem Soda = new WarehouseItem("Soda", 10, "/Images/Add.png");
                data.Add(Soda);

                return data;
            }
        }

        public void BindData()
        {
           this.radTreeListView.ItemsSource = WarehouseService.GetWarehouseData();
        }

    }
}

期望的输出: enter image description here

0 个答案:

没有答案