I have a problem binding Song title and artist to SongView usercontrol inside the listview, here's the code:
StartPage.xaml
<ListView x:Name="PopularSongs" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Song.Title}"/>
<local:SongView
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Song="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
StartPage.cs
public StartPage()
{
this.InitializeComponent();
this.DataContext = this;
vm.NewSongs.Add( new Song()
{
Artist = "Scorpions",
Title = "Wind of Change",
ImageURL = "http://images.45cat.com/scorpions-wind-of-changed-mercury.jpg",
Length = 2.56,
Size = 3.3
} );
vm.NewSongs.Add(new Song()
{
Artist = "Omega",
Title = "Gyongihau Lany",
ImageURL = "http://img11.nnm.me/b/d/c/b/7/bdcb701dbca19fde7fed545403f769fb_full.jpg",
Length = 5.32,
Size = 12
});
PopularSongs.ItemsSource = vm.NewSongs;
}
Here, vm.NewSongs is ObservableCollection of type Song
SongView.xaml
<UserControl
x:Class="soldo.SongView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:soldo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="60"
d:DesignWidth="300"
x:Name="root">
<Grid Background="{StaticResource ExtraLightGreyBrush}" Margin="0,3,0,0" HorizontalAlignment="Stretch"> <StackPanel Grid.Column="1" Margin="5">
<TextBlock FontFamily="{StaticResource OpenSansRegular}"
Text="{Binding ElementName=root, Path=Song.Title, FallbackValue=Title}"
Foreground="{StaticResource BlackBrush}"
FontSize="24"/>
<TextBlock FontFamily="{StaticResource OpenSansBold}"
Text="{Binding ElementName=root, Path=Song.Artist, FallbackValue=Artist}"
FontSize="15"
Margin="0,-5,0,0"
Foreground="{StaticResource OrangeBrush}"/>
</Grid>
</UserControl>
SongView.cs
public static readonly DependencyProperty SongProperty = DependencyProperty.Register(
"Song",
typeof(Song),
typeof(SongView),
new PropertyMetadata(string.Empty)
);
public Song Song
{
get
{
return GetValue(SongProperty) as Song;
}
set
{
SetValue(SongProperty, value);
}
}
In output are some binding errors, but i have no idea why they appear
{Binding Path=Title}
– Kevin Gosse