2012-12-17 26 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