Search code examples
xmlwpflistboxdatatemplate

how to apply data template on listbox


I am having a data template,and trying to apply it on a list box,i have textbox,labels and a button on the template,but it isnt showing up on the listbox,i dont have any databinding yet but still it must show the textbox,labels,button but it isnt showing up, here is the code for datatemplate it is used as a resource

<Window.Resources>
            <DataTemplate x:Key="tasktemplate1">
                <Canvas Height="50" Width="850" Background="lightgray">
                    <Label   Height="30" Width="170" Canvas.Top="10" Canvas.Left="150" Background="LightGray">
                    </Label>
                    <TextBox Height="30" Width="60" Canvas.Top="10" Canvas.Left="370" Background="Black"></TextBox>
                    <Label Canvas.Left="500" Canvas.Top="10">$</Label>
                    <Button Click="deletebuttonclick" Canvas.Top="12" Height="10" Width="30" Canvas.Left="600" ></Button>
                </Canvas>
            </DataTemplate>
        </Window.Resources>

and the code for my listbox

<TabItem>
        <Canvas Height="700" Width="850">
            <ListBox ItemTemplate="{StaticResource tasktemplate1}" ItemsSource="{Binding}" x:Name="listBox" Height="700" Width="850">
            </ListBox>
            <Label Canvas.Top="-18" Canvas.Left="185">Select Task</Label>
            <Label Canvas.Top="-18" Canvas.Left="377" RenderTransformOrigin="0.58,0.462">Enter Bill Rates</Label>
            <Button Canvas.Left="39" Canvas.Top="575" Width="139">Click to add the task</Button>
        </Canvas>
    </TabItem>

where am i going wrong?any helps,thanx


Solution

  • DataTemplate is used to represent visual layout for listBoxItems. But, ItemsSource collection is null or count is 0, (assuming because you set it to just Binding) hence no items are generate and shown in your listBox. You need to pass ItemsSource with some objects in it so that DataTemplate gets applied to ListBoxItems.

    For testing you can define ItemsSource in XAML itself:

    <Canvas Height="700" Width="850">
        <Canvas.Resources>
            <ObjectDataProvider x:Key="EnumerableRange"
                xmlns:sys="clr-namespace:System;assembly=mscorlib"
                xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
                ObjectType="{x:Type linq:Enumerable}" MethodName="Range">
                <ObjectDataProvider.MethodParameters>
                    <sys:Int32>1</sys:Int32>
                    <sys:Int32>10</sys:Int32>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Canvas.Resources>
        <ListBox ItemTemplate="{StaticResource tasktemplate1}"
                 ItemsSource="{Binding Source={StaticResource EnumerableRange}}" 
                 x:Name="listBox" Height="700" Width="850"/>
         ......
    </Canvas>
    

    In the sample above i basically provide an integer collection with 10 items as ItemsSource. So, you will see 10 templates for it. In case you want to see more of the items, go and update the number from 10 to some other value in resource EnumerableRange.