1

所以,我在 WPF 中有以下绑定。

    <GroupBox Header="Nuværende Sang" HorizontalAlignment="Left" Margin="20,23,0,0" VerticalAlignment="Top" Height="165" Width="288">
        <Grid HorizontalAlignment="Left" Height="143" VerticalAlignment="Top" Width="276">
            <Grid.DataContext>
                <ETC:Song/>
            </Grid.DataContext>
            <Image HorizontalAlignment="Left" Height="124" Margin="10,10,0,0" VerticalAlignment="Top" Width="134" Stretch="Fill"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,100,0,0" TextWrapping="Wrap" Text="{Binding Time}" VerticalAlignment="Top" Height="25" Width="122"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,40,0,0" TextWrapping="Wrap" Text="{Binding Artist}" VerticalAlignment="Top" Height="25" Width="122"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,70,0,0" TextWrapping="Wrap" Text="{Binding Album}" VerticalAlignment="Top" Height="25" Width="122"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,10,0,0" TextWrapping="Wrap" Text="{Binding Title}" VerticalAlignment="Top" Height="25" Width="122"/>
        </Grid>
    </GroupBox>

值总是返回null,我知道字符串和 int 不是null,因为它们已在此 ListView 中使用:

        <ListView x:Name="ListViewSange" HorizontalAlignment="Left" Height="436" VerticalAlignment="Top" Width="984" Background="{x:Null}" ItemsSource="{Binding Songs}" FontFamily="Malgun Gothic" FontSize="24" Margin="0,0,0,-12">
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridViewColumn Header="No." DisplayMemberBinding="{Binding ActiveIndex}" Width="50"/>
                    <GridViewColumn Header="Song name" DisplayMemberBinding="{Binding Title}" Width="700"/>
                    <GridViewColumn Header="Artist" DisplayMemberBinding="{Binding Artist}" Width="234"/>
                </GridView>
            </ListView.View>
        </ListView>

我错过了什么?

他们都指的是这个类:

public class Song
{
    public string Title { get ; set; }
    public string Path { get; set; }
    public string Artist { get; set; }
    public int Time { get; set; }
    public string Album { get; set; }
    //public ImageBrush Portrait { get; set; }


    public Song(string title, string path, string artist, int time, string album)//, ImageBrush portrait)
    {
        this.Path = path;
        this.Title = title;
        this.Artist = artist;
        this.Time = time;
        this.Album = album;
        //this.Portrait = portrait;
    }

    public Song()
    {
        // TODO: Complete member initialization
    }
4

1 回答 1

1

通过使用 :

<Grid.DataContext>
    <ETC:Song/>
 </Grid.DataContext>

Song您正在为您的 Grid分配一个空实例DataContext。由于DataContext是从父级继承的,因此您TextBlock可能会绑定到来自空 Song 实例的空值。

删除这段代码,并使用您真正想要显示DataContext的实例进行设置。Song

如果您ListView和您Grid位于同一页面中,我建议您创建一个专用属性(在同一个类中,并且就在List<Song>绑定到您的属性ListView旁边SongCurrentlyPlaying,例如)并以TextBlock这种方式绑定您:

   <TextBlock HorizontalAlignment="Left" Margin="144,100,0,0" TextWrapping="Wrap" Text="{Binding SongCurrentlyPlaying.Time}" VerticalAlignment="Top" Height="25" Width="122"/>
   <TextBlock HorizontalAlignment="Left" Margin="144,40,0,0" TextWrapping="Wrap" Text="{Binding SongCurrentlyPlaying.Artist}" VerticalAlignment="Top" Height="25" Width="122"/>
   <TextBlock HorizontalAlignment="Left" Margin="144,70,0,0" TextWrapping="Wrap" Text="{Binding SongCurrentlyPlaying.Album}" VerticalAlignment="Top" Height="25" Width="122"/>
   <TextBlock HorizontalAlignment="Left" Margin="144,10,0,0" TextWrapping="Wrap" Text="{Binding SongCurrentlyPlaying.Title}" VerticalAlignment="Top" Height="25" Width="122"/>

不要忘记这个新属性必须使用INotifyPropertyChanged或作为DependancyProperty,以便它可以在其值发生更改时通知视图(和绑定);)

于 2013-10-24T20:59:30.850 回答