动态绑定更新映像

时间:2013-01-25 06:55:21

标签: c# wpf xaml

我有一个堆叠了标题和描述的图像列表(ListBox)。图像尚未下载,但标题和说明将首先显示。下载图像时,如何更新图像?

部分xaml:

<ListBox.ItemTemplate>
   <DataTemplate>
       <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Image Margin="5" Source="{Binding Image}" Grid.Column="0" Name="DCIM" />
            <TextBlock Grid.Column="1" Margin="2" Text="{Binding Title}" Name="Title" TextWrapping="NoWrap" TextTrimming="WordEllipsis" />
            <TextBlock Grid.Column="1" Margin="2" Text="{Binding Desc}" Name="count" TextWrapping="NoWrap" TextTrimming="WordEllipsis" />
       </Grid>
   </DataTemplate>
</ListBox.ItemTemplate>

2 个答案:

答案 0 :(得分:0)

下载图片后,请致电NotifyProperyChanged("Image")以更新Source="{Binding Image}"

答案 1 :(得分:0)

您需要在商品类中实施INotifyPropertyChanged

public class MyDataItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ImageSource image;
    public ImageSource Image
    {
        get { return image; }
        set
        {
            image = value;
            NotifyPropertyChanged("Image");
        }
    }

    // do the same for Title and Desc

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

分配Image属性时,会引发属性更改通知,更新绑定。