I have an ItemsControl whose ItemsSource is bound to an XML data provider. The code looks like this.
<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 XPath=@Name}"
ToolTip="{Binding XPath=Description}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
VideosXML is an XML data provider referencing an external XML file. As you can see the Name attributes apply to the buttons’ content and the Description elements in the xml file apply to the tooltips of the buttons. Below is the code for the button’s style. It basically is a textblock with a faded “Play” button over the top of it.
<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>
You can see from the style that the “Text” in the TextBlock is bound to the content of the button itself: Text=”{TemplateBinding Content}”, and from the first piece of code the Content of the button is bound to an XML element via XPath. However, no text is shown at all. It works if I hard code something in the button like Content=”A Button” will show. Also the tooltip is working fine so I know its reading data from the XML file. So what makes binding to XPath different than hard coding a value?
Thanks in advance for looking at my question!
EDIT: Sample 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>
Ok, I found the problem, for some reason the
Contentproperty is passing the whole xml element, setting the Content binding toContent="{Binding XPath=@Name, Path=Value}"should fix the issueThere is probably a logical explaination for this, Google will know