绑定到ObservableCollection的ListBox未在加载的视图上刷新

时间:2013-09-07 04:09:16

标签: wpf data-binding listbox observablecollection

我有以下XAML包含绑定到ObservableCollection

的ListBox
 <ListBox 
        Margin="0,5" 
        Grid.Row="1" 
        Grid.ColumnSpan="3" 
        Visibility="{Binding ArePicturesAvailable, Converter={StaticResource BoolToVisConv}}"
        SelectedItem="{Binding SelectedPicture}"
        ItemsSource="{Binding Pictures, NotifyOnSourceUpdated=True}"
        ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Margin="8" 
                       Height="{Binding Size.Height}"
                       Width="{Binding Size.Width}"
                       Source="{Binding FullPath, Converter={StaticResource RelativeToFullConv}}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

由于填充ListBox有明显的延迟,我试图推迟这个,直到使用Dispatcher.BeginInvoke加载ListBox的包含视图,如下所示:

public class PictureMarkerEditorViewModel
{
    private PictureSelectorViewModel _pictureSelectorViewModel;

    public string PhotoCollectionDirectory { get; set; }

    protected ISymbolEditor GetEditorImpl(ISymbolInfo symbolInfo)
    {
        var picSymbolInfo = (PictureMarkerSymbolInfo)symbolInfo;
        _pictureSelectorViewModel = new PictureSelectorViewModel(picSymbolInfo);

        var editor = new PictureMarkerSymbolEditor(_pictureSelectorViewModel, picSymbolInfo);          
        editor.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            var photos = GetPhotoCollection();
            _pictureSelectorViewModel.Pictures.AddRange(photos);
        }));

        //var photos = GetPhotoCollection();
        //_pictureSelectorViewModel.Pictures.AddRange(photos);

        return editor;
    }    

    public IEnumerable<Picture> GetPhotoCollection()
    {
        if (string.IsNullOrEmpty(PhotoCollectionDirectory))
            return null;

        if (!Directory.Exists(PhotoCollectionDirectory))
            throw new ArgumentException("Cannot show images from invalid directory " + PhotoCollectionDirectory + ".");

        var files = Directory.GetFiles(PhotoCollectionDirectory, "*.png", SearchOption.AllDirectories);
        return files.Select(f => new Picture(Path.GetFileName(f), f, ImageUtils.GetDimensions(f)));
    }
}

其中PictureSelectorViewModel.Pictures只是图片的ObservableCollection:

public class PictureSelectorViewModel
{
  private readonly ObservableCollection<Picture> _pictures= new ObservableCollection<Picture>();
        public ObservableCollection<Picture> Pictures 
        {
            get
            {                   
                return  _pictures;
            }
        }
}

和Picture是一个包含Name和FullPath属性的简单类:

  public class Picture
    { 
       public string Name { get;set} 
       public string FullPath {get; set;}
       public Size Size {get;set;}
    }

我只是无法在加载后填充列表。如果我在加载视图之前填充图片集合(代码的注释部分,我会看到图像。

任何人都知道为什么?

TIA。

0 个答案:

没有答案