2013-10-24 97 views
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 
    } 
+0

你是否也在你的列表視圖的頁面中使用? – AirL

+0

不,我不是,因此the ItemsSource =「{Binding Songs}」:) :) –

+0

好吧那麼是指什麼?可以發佈相關的代碼? – AirL

回答

1

使用:

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

您對您的電網DataContext分配一個空Song實例。由於DataContext從父項繼承,因此您的TextBlock可能會綁定到來自空的Song實例的空值。

刪除這段代碼,並將您想要顯示的Song實例設置爲DataContext

如果您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因此它可以通知視圖(和綁定)時,它的價值已經改變;)

+0

坦克很多!會投票給你,但錯過了:) –

+0

不客氣;) – AirL