2012-12-17 22 views
0

我有一个ItemsControl的ItemsSource绑定到XML数据提供者。代码看起来像这样。TemplateBinding父级的内容本身绑定到XPath - 内容不显示

<ItemsControl Grid.Row="1" Margin="30" 
       ItemsSource="{Binding Source={StaticResource VideosXML}, 
       XPath=TutorialVideo}" > 
    <ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel/> 
    </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <Button Style="{StaticResource StyleMetroVideoButton}" 
       Content="{Binding [email protected]}" 
       ToolTip="{Binding XPath=Description}"/> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate>       
</ItemsControl> 

VideosXML是引用外部XML文件的XML数据提供者。正如你所看到的,Name属性适用于按钮的内容,并且xml文件中的Description元素应用于按钮的工具提示。以下是按钮样式的代码。它基本上是一个文本块,在其顶部有一个淡化的“播放”按钮。

<Style TargetType="{x:Type Button}" x:Key="StyleMetroVideoButton"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid Name="PlayGrid" Background="#FF323236"> 
         <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Content}" VerticalAlignment="Top" HorizontalAlignment="Center"/> 
         <Image Name="Play" Source="{StaticResource BtnVideoPlayHoverPNG}" Opacity="0.0" Stretch="None"/> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Opacity" Value="0.6" TargetName="Play"/> 
          <Setter Property="Background" Value="#8D8D94" TargetName="PlayGrid"/> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="True"> 
          <Setter Property="Source" Value="{StaticResource BtnVideoPlayClickPNG}" TargetName="Play"/> 
          <Setter Property="Opacity" Value="0.6" TargetName="Play"/> 
          <Setter Property="Background" Value="#8D8D94" TargetName="PlayGrid"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="False"> 
          <Setter Property="Opacity" Value="0.0" TargetName="Play"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Width" Value="90" /> 
     <Setter Property="Height" Value="80" /> 
     <Setter Property="Margin" Value="5,5,5,5"/> 
    </Style> 

您可以从“文本”中的TextBlock绑定到按钮本身的内容风格看:文本=“{TemplateBinding内容}”,并从第一段代码的内容该按钮通过XPath绑定到XML元素。但是,根本没有显示文字。如果我在像Content =“A Button”这样的按钮中显示硬编码的东西,它会起作用。此外,工具提示工作正常,所以我知道它从XML文件中读取数据。那么,与XPath绑定有什么不同,而不是硬编码一个值呢?

在此先感谢您看我的问题!

编辑:示例XML

<?xml version="1.0" encoding="utf-8" ?> 
<Videos xmlns=""> 
    <TutorialVideo Name="Video 1"> 
    <Description>A video to watch</Description> 
    <Filepath>video1.wmv</Filepath> 
    </TutorialVideo> 
</Videos> 
+0

发生了什么我你用'ContentPresenter'替换'TextBlock'(只是猜测),如果你发布一个小的XML例子,我可以看看。 –

+0

添加ContentPresenter时没有任何变化。我把一些示例xml起来。谢谢! – akagixxer

+0

它工作,如果你在TextBlock Text =“{Binding XPath = @ Name}”''这样看来传递的内容是整个XML元素,我会继续寻找 –

回答

1

好吧,我发现这个问题,由于某些原因,Content属性是通过整个XML元素,设置内容结合Content="{Binding [email protected], Path=Value}"应该解决这个问题

这可能是一个逻辑的解释,谷歌将知道

<ItemsControl Grid.Row="1" Margin="30" 
       ItemsSource="{Binding Source={StaticResource VideosXML}, 
       XPath=TutorialVideo}" > 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <WrapPanel/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Style="{StaticResource StyleMetroVideoButton}" 
          Content="{Binding [email protected], Path=Value}" 
          ToolTip="{Binding XPath=Description}"/> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
+0

哦,好的,谢谢!嗯,它正在工作;但是,显示的内容是Button的内容本身。我可以从样式中删除整个TextBlock,并且它仍然会显示,因为内容没有传递给TextBlock的Text。我需要这个的原因是因为TextBlock可以包装文本,按钮不能。 – akagixxer

+0

更新的答案,它似乎如果您将绑定路径设置为值,它工作正常,我的事情与ContentPresenter不知道你想要的xml元素的字符串值。 –

+0

不错!我很感激帮助。我并不想挑剔,但现在Button内容和TextBlock文本都显示并重叠。也许我以错误的方式接近这个想法?我把TextBlock作为按钮内容,并且工作;然而,这并不能给我带来这种风格的灵活性......对不起,我还在学习xaml以及它是如何工作的。 – akagixxer