0

我有一个 xml 文件,我正在尝试将它绑定到标签但尚未成功,我使用 xmldataprovider 来绑定它。这是我使用的模板的代码。,我的 xml 文件中有几个任务,我是试图绑定第一个。

 <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>

并且使用模板的列表框的代码在这里

 <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>

我没有找到我的绑定出错的地方,任何帮助,谢谢。

4

1 回答 1

1

您不能在 ListBox 上同时设置ItemsSource和手动添加。ListBoxItem指定 ItemsSource 或根据您的条件添加项目。

在您的情况下,如果您只想显示第一项,请删除并ItemsSource 设置DataContext.ListBoxItem

<ListBox x:Name="listBox" Height="700" Width="850">
    <ListBoxItem DataContext="{Binding Source={StaticResource TaskList}}"
                 Template="{StaticResource tasktemplate1}"/>
</ListBox>
于 2014-06-16T11:45:08.913 回答