无法将信息连接到列表框,在WP7中还有一个列表框

时间:2012-12-02 05:17:37

标签: c# wpf windows-phone-7

我有一堂课新闻

public class News : ObservableCollection<New>
{
    public News()
        : base()
    {

    }
}

新课程

public class New : INotifyPropertyChanged
{
         public PhotoAttachments Photo
    {
        get
        { 
            return photoAttachments;
        }
        set
        {
            photoAttachments = value;
            OnPropertyChanged("Photo");
        }
    }
   // some fields such as date, text, id, sourceName etc
 public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged(string info)
{//realisation of method}
 public PhotoAttachments photoAttachments = new PhotoAttachments(); // it is a collection, such as News, but it contains objects of class PhotoAttachment, which have property with string url to photo
}

InitializeComponent();我写this.listBox.ItemsSource = NewsList; 所以有一个列表框,其中包含New类的对象。 在这些列表框中,我创建了另一个列表框,并尝试通过PhotoAttachments集合填充它。在这里我有一个问题,带有照片的列表框不显示照片(但它们存在)。这是XAML:

// I can select different      <local:NewsTemplateSelector.Photos>
//style of listbox               <DataTemplate>
//using  NewsTemplateSelector     <Border BorderBrush="Red" BorderThickness="2"  Width="400" Height="300" Margin="10">
                                        <StackPanel Orientation="Horizontal" Width="400" Height="300">
                                            <Image Source="{Binding SourceImage}" Height="75" Width="75" Margin="0,-225,0,0" />
                                            <Canvas Width="400">
                                                <TextBlock Text="{Binding SourceName}" Foreground="Black" FontSize="25" TextWrapping="Wrap" Height="65" Width="326" d:LayoutOverrides="VerticalAlignment, Height" />
                                                <ListBox Name="photoListbox" ItemsSource="{Binding Photo}"  Height="229" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="326" Canvas.Top="69">
                                                    <Image  Source="{Binding Big}" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="400" />
                                                </ListBox>
                                            </Canvas>
                                        </StackPanel>
                                    </Border>
                                </DataTemplate>
                            </local:NewsTemplateSelector.Photos>

PhotoAttachment class:

    public class PhotoAttachment : INotifyPropertyChanged
{
    private string ownerId;
    public string OwnerId { get { return ownerId; } set { ownerId = value; OnPropertyChanged("OwnerId"); } }
    private string small;
    public string Small { get { return small; } set { small = value; OnPropertyChanged("Small"); } }
    private string big;
    public string Big { get { return big; } set { big = value; OnPropertyChanged("Big"); } }
    public PhotoAttachment(string ownId, string small, string big)
    {
        ownerId = ownId;
        this.small = small;
        this.big = big;
    }
    public PhotoAttachment() { }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }

}

1 个答案:

答案 0 :(得分:1)

刚刚意识到你的PhotoListView的XAML缺少一个ItemTemplate

这些方面的东西应该可以解决问题:

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image  Source="{Binding Big}" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="400" />
            </DataTemplate>
        </ListBox.ItemTemplate>