wpftemplateslistboxlistbox-control

how to bind .xml file content on a label stored on a url


i have an xml file ,i am trying to bind it to a label but not successfull yet,i used an xmldataprovider to bind it.here is the code for the template i used.,there are several task in my xml file,i am trying to bind the 1st one.

 <Window.Resources>
        <XmlDataProvider x:Key="TaskList" Source="http://store.tymesheet.com/templates/Graphic-Designer.xml" XPath="tasks"/>
        <ControlTemplate x:Key="tasktemplate1">
            <Canvas Height="40" Width="850">
                <Label Content="{Binding XPath=task[1]}"  Height="30" Width="170" Canvas.Top="5" Canvas.Left="150" Background="LightGray">

                </Label>
                <TextBox Height="30" Width="120" Canvas.Top="5" Canvas.Left="370" Background="AliceBlue"></TextBox>
                <Label Canvas.Left="500" Canvas.Top="5">$</Label>
                <Button Canvas.Top="8" Height="10" Width="30" Canvas.Left="600" ></Button>
            </Canvas>
        </ControlTemplate>
        </Window.Resources>

and the code for the listbox where the template is used is here

 <TabItem>
        <Canvas Height="700" Width="850">
            <ListBox ItemsSource="{Binding Path=TaskList}" x:Name="listBox" Height="700" Width="850">
                    <ListBoxItem  Template="{StaticResource tasktemplate1}"/>
            </ListBox>
        </Canvas>
    </TabItem>

i am not getting where my binding is going wrong,any help,thnx.


Solution

  • You can't set ItemsSource and manually add ListBoxItem at same time on ListBox. Either specify ItemsSource or add items based on your condition.

    In your case if you want to show only first item, remove ItemsSource and set DataContext of ListBoxItem.

    <ListBox x:Name="listBox" Height="700" Width="850">
        <ListBoxItem DataContext="{Binding Source={StaticResource TaskList}}"
                     Template="{StaticResource tasktemplate1}"/>
    </ListBox>