locked
HierarchicalDataTemplate RRS feed

  • Question

  • Hi everyone

     Here itemscontrol is custom control.

    Source classes:

    Class Sales(String Name,double Salesdet, double Expense)

    Class SalesCategory(string Category,Sales SalesDetails)

     

    Inside the constructor of my usercontrol. i have placed following coding

    ObservableCollection<SalesCategory> salescategories = new ObservableCollection<SalesCategory>(); ObservableCollection<Sales> sales = new ObservableCollection<Sales>();

    sales.Add(new Sales() { Name = "Indiana", Salesdet = 100, Expense = 100, Country = "US" });

    sales.Add(new Sales() { Name = "Chicago", Salesdet = 58, Expense = 58746, Country = "US" });salescategories.Add(new SalesCategory("US", sales));

     

    sales.Add(
    new Sales() { Name = "New jersey", Salesdet = 35, Expense = 35, Country = "Canada" });

    sales.Add(new Sales() { Name = "Toronto", Salesdet = 79, Expense = 46, Country = "Canada" });

    salescategories.Add(new SalesCategory("CANADA", sales));

    this.ItemscontrolName.ItemsSource = salescategories;

     

    In xaml

     

    <Itemscontrol>

    <Itemscontrolname.ItemContainerStyle >

    <Style TargetType="Itemscontrolitemname">

    <Setter Property="sales" Value="Salesdet"></Setter>

    <Setter Property="expenses" Value="Expense"/>

    </Style>

    </itemscontrolname.ItemContainerStyle>

     

     

    <itemscontrolname.ItemTemplate>

    <toolkit:HierarchicalDataTemplate ItemsSource="{Binding SalesCategory}" >

    <TextBlock FontWeight="Bold" Text="{Binding Category}"></TextBlock>

    <toolkit:HierarchicalDataTemplate.ItemTemplate >

    <DataTemplate >

    <Border BorderBrush="Aqua" BorderThickness="5">

    <TextBlock Text="{Binding Name}"/>

    </Border>

    </DataTemplate>

    </toolkit:HierarchicalDataTemplate.ItemTemplate>

    </toolkit:HierarchicalDataTemplate>

    </Itemscontrolname.ItemTemplate>

    </Itemscontrolnamel>

     

     In xaml it is showing following error   "AGE unknown error" and nothing is displaying inside the itemscontrol

    can anyone help me.

    Than ks

    Renu

     

     

              

    Thursday, November 6, 2008 8:33 AM

Answers

  • 1. You can't nest a TextBlock directly in HierarchicalDataTemplate. Direct nesting in a HierarchicalDataTemplate sets the ItemTemplate. which you're already doing in the next line.

    2. You don't actually need a HierarchicalDataTemplate for this sample since there's nothing Hierarchical about it.

     

    All and all, it looks like you're trying to display a list of non-hierarchical items with a header.
    Instead of using an ItemsControl with an HierarchicalDataTemplate, use a HeaderedItemsControl from the Silverlight Toolkit.

    Basically, Just set the HeaderTemplate/Header to get your Header text and the ItemTemplate to display your items.

    <controls:HeaderedItemsControl Header="{Binding}" ItemsSource="{Binding Sales}">

        <controls:HeaderedItemsControl.HeaderTemplate>

            <DataTemplate>

                <TextBlock Text="{Binding Category}" />

            </DataTemplate>

        </controls:HeaderedItemsControl.HeaderTemplate>

        <controls:HeaderedItemsControl.ItemTemplate>

            <DataTemplate>

                <TextBlock Text="{Binding Name}" />

            </DataTemplate>

        </controls:HeaderedItemsControl.ItemTemplate>

    </controls:HeaderedItemsControl>

    Thursday, November 6, 2008 2:18 PM

All replies

  • There are lot of Typing issues(not sure you just typed this in the post). what exactly are you trying to do

    Thursday, November 6, 2008 9:12 AM
  • Hi,

     

    Yes i have typed, so there may some typing mistakes.

     

    Renu

    Thursday, November 6, 2008 9:15 AM
  • 1. You can't nest a TextBlock directly in HierarchicalDataTemplate. Direct nesting in a HierarchicalDataTemplate sets the ItemTemplate. which you're already doing in the next line.

    2. You don't actually need a HierarchicalDataTemplate for this sample since there's nothing Hierarchical about it.

     

    All and all, it looks like you're trying to display a list of non-hierarchical items with a header.
    Instead of using an ItemsControl with an HierarchicalDataTemplate, use a HeaderedItemsControl from the Silverlight Toolkit.

    Basically, Just set the HeaderTemplate/Header to get your Header text and the ItemTemplate to display your items.

    <controls:HeaderedItemsControl Header="{Binding}" ItemsSource="{Binding Sales}">

        <controls:HeaderedItemsControl.HeaderTemplate>

            <DataTemplate>

                <TextBlock Text="{Binding Category}" />

            </DataTemplate>

        </controls:HeaderedItemsControl.HeaderTemplate>

        <controls:HeaderedItemsControl.ItemTemplate>

            <DataTemplate>

                <TextBlock Text="{Binding Name}" />

            </DataTemplate>

        </controls:HeaderedItemsControl.ItemTemplate>

    </controls:HeaderedItemsControl>

    Thursday, November 6, 2008 2:18 PM
  • Hi Justin,

     Thanks for replying....

    I meant to display Hierarchical data, If the code i have provided doesnt give hierarchical meaning, can u please provide me sample code determining how the hierarchical data should be and also how to display the hierarchical data. URGENT 

     

    Renu

    Thursday, November 6, 2008 9:59 PM