我有一個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>
發生了什麼我你用'ContentPresenter'替換'TextBlock'(只是猜測),如果你發佈一個小的XML例子,我可以看看。 –
添加ContentPresenter時沒有任何變化。我把一些示例xml起來。謝謝! – akagixxer
它工作,如果你在TextBlock Text =「{Binding XPath = @ Name}」''這樣看來傳遞的內容是整個XML元素,我會繼續尋找 –