Databinded列表框仅绑定第一项

时间:2010-07-27 10:58:22

标签: wpf

我有以下xml文件:

<CUEShow>
  <Showtitel>example show</Showtitel>
  <Authortitel>Wouter</Authortitel>





  <Singlecues>
<Singlecue><nr>1</nr><Name>Onweer</Name></Singlecue>
<Singlecue><nr>2</nr><Name>Regen</Name></Singlecue>
<Singlecue><nr>3</nr><Name>Donder</Name></Singlecue>
<Singlecue><nr>4</nr><Name>Onweer</Name></Singlecue>
<Singlecue><nr>5</nr><Name>Regen</Name></Singlecue>
<Singlecue><nr>6</nr><Name>Donder</Name></Singlecue>
<Singlecue><nr>7</nr><Name>Onweer</Name></Singlecue>
<Singlecue><nr>8</nr><Name>Regen</Name></Singlecue>
<Singlecue><nr>9</nr><Name>Donder</Name></Singlecue>
  </Singlecues>

  <Maincues>
<Maincue><nr>1</nr><Name>Onweer</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>2</nr><Name>Regen</Name><Duration>Lang</Duration><Type>Follow</Type></Maincue>
<Maincue><nr>3</nr><Name>Donder</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>4</nr><Name>Onweer</Name><Duration>Lang</Duration><Type>Follow Direct</Type></Maincue>
<Maincue><nr>5</nr><Name>Regen</Name><Duration>Lang</Duration><Type>Follow</Type></Maincue>
<Maincue><nr>6</nr><Name>Donder</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>7</nr><Name>Onweer</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>8</nr><Name>Regen</Name><Duration>Lang</Duration><Type>Follow Direct</Type></Maincue>
<Maincue><nr>9</nr><Name>Donder</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>10</nr><Name>Onweer</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>11</nr><Name>Regen</Name><Duration>Lang</Duration><Type>Follow</Type></Maincue>
<Maincue><nr>12</nr><Name>Donder</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>13</nr><Name>Onweer</Name><Duration>Lang</Duration><Type>Follow Direct</Type></Maincue>
<Maincue><nr>14</nr><Name>Regen</Name><Duration>Lang</Duration><Type>Follow</Type></Maincue>
<Maincue><nr>15</nr><Name>Donder</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>16</nr><Name>Onweer</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
<Maincue><nr>17</nr><Name>Regen</Name><Duration>Lang</Duration><Type>Follow Direct</Type></Maincue>
<Maincue><nr>18</nr><Name>Donder</Name><Duration>Lang</Duration><Type>GO</Type></Maincue>
  </Maincues>

  <Mastervolume Value="75" />


</CUEShow>

我已将该文件绑定为数据源。现在我有一个列表框,我想在其中显示所有Maincue节点。我去了contentsource并选择了Maincue节点。然后我创建了一个itemdatatemplate,其中我将一个名称绑定到一个文本框,nr指向一个按钮文本,但列表只显示了第一个maincue。见下文:

alt text http://tegen-licht.nl/Screenie.png

我的项目datatemplate如下:

<DataTemplate x:Key="Maincuestemplate">
        <Grid Height="30" Width="958.737" DataContext="{Binding XPath=/CUEShow/Maincues}" Margin="0">
            <Button Content="{Binding XPath=/CUEShow/Maincues/Maincue/nr}" Width="30" Style="{DynamicResource CUEStyle_Button_Inhoudknopje}" Template="{DynamicResource CUEStyle_Knopje_Cuelijsten}" Height="Auto" HorizontalAlignment="Left" Background="#FFABCCED" DataContext="{Binding XPath=Maincue/nr}"/>
            <TextBlock Margin="58,0,0,0" Width="289.07" VerticalAlignment="Center" FontSize="16" Foreground="Gray" Text="{Binding XPath=/CUEShow/Maincues/Maincue/Name}" HorizontalAlignment="Left"/>
            <Button Width="30" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_Rondknopje}" Height="Auto" HorizontalAlignment="Right" Margin="0,0.55,74.737,-0.55" Content="u" FontFamily="Wingdings 3" Foreground="Gray" Opacity="0.4"/>
            <Button Width="30" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_Rondknopje}" Height="Auto" HorizontalAlignment="Right" Margin="0,0.55,37.071,-0.55" Content="¢" FontFamily="Wingdings 2" Foreground="Gray" Opacity="0.4"/>
            <Button Width="30" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_Rondknopje}" Height="Auto" HorizontalAlignment="Right" Margin="0" Content="Q" FontFamily="Wingdings 3" Foreground="Gray" FontWeight="Bold" FontSize="18.667" Opacity="0.4"/>
            <ProgressBar Style="{DynamicResource CUEStyle_Progressbar}" Panel.ZIndex="-1" Margin="361.93,10.833,401.737,6.527" Value="35" Template="{DynamicResource CUE_Progress}" RenderTransformOrigin="0.5,0.5">
                <ProgressBar.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform ScaleY="-1"/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </ProgressBar.RenderTransform>
            </ProgressBar>
            <TextBlock Margin="421,4.421,456.667,4.298" Width="Auto" VerticalAlignment="Stretch" FontSize="16" Foreground="Gray" Text="{Binding XPath=/CUEShow/Maincues/Maincue/Duration}" HorizontalAlignment="Stretch" d:LayoutOverrides="Height"/>
            <TextBlock Margin="0,0.55,166.667,0.718" Width="81.07" VerticalAlignment="Stretch" FontSize="21.333" Foreground="Gray" Text="{Binding XPath=/CUEShow/Maincues/Maincue/Type}" HorizontalAlignment="Right"/>
        </Grid>
    </DataTemplate>

我做错了什么,绑定路径似乎是正确的......

1 个答案:

答案 0 :(得分:1)

当您使用斜杠启动XPath表达式时,它将相对于文档的根目录起作用。因此,无论文档中的当前节点如何,{Binding XPath=/CUEShow/Maincues/Maincue/nr}都将评估文档中的每个nr节点。由于它需要单个节点,因此框架将只占用第一个节点,并且模板的每个实例都将使用第一个节点的内容。

我假设您在ItemsControl中拥有此DataTemplate,并且数据上下文是Maincue节点。在这种情况下,从Grid和Button中删除DataContext属性,因为您要使用ItemsControl提供的DataContext,并从每个XPath表达式中删除/CUEShow/Maincues/Maincue/,以便从当前节点开始。例如,<Button Content="{Binding XPath=nr}"