C# 将图像绑定到ImageBrush
尝试将图像绑定到ImageSource时出现问题。我在stackoverflow上尝试了其他一些修复方法,但没有一个有效 我似乎在这行中得到一个错误,说集合“Items”必须为空C# 将图像绑定到ImageBrush,c#,xaml,C#,Xaml,尝试将图像绑定到ImageSource时出现问题。我在stackoverflow上尝试了其他一些修复方法,但没有一个有效 我似乎在这行中得到一个错误,说集合“Items”必须为空 ImageList.ItemsSource = List; 当使用FlickrData类的“url”成员时,绑定工作得很好 main window.xaml <ScrollViewer> <ListView x:Name="ImageList" ScrollViewer.H
ImageList.ItemsSource = List;
当使用FlickrData类的“url”成员时,绑定工作得很好
main window.xaml
<ScrollViewer>
<ListView x:Name="ImageList" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<Rectangle Margin="5" Width="100" Height="100">
<Rectangle.Fill>
<ImageBrush ImageSource="{Binding imageBinding}"/>
</Rectangle.Fill>
</Rectangle>
</ListView>
</ScrollViewer>
主窗口类:
public class FlickrData
{
public String url { get; set;}
public FlickrData(Photo photo)
{
url = photo.SmallUrl;
}
public ImageBrush imageBinding
{
get
{
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(url));
return brush;
}
}
}
public partial class MainWindow : Window
{
public ObservableCollection<FlickrData> List = new ObservableCollection<FlickrData>();
public static Flickr flickr = new Flickr("XXXXXXXXXXXXXX");
public MainWindow()
{
InitializeComponent();
}
public void SearchWithInput(object sender, RoutedEventArgs e)
{
var options = new PhotoSearchOptions { Tags = SearchInput.Text, PerPage = 20, Page = 1 };
PhotoCollection photos = flickr.PhotosSearch(options);
List.Clear();
foreach (Photo photo in photos)
{
String flickrUrl = photo.WebUrl;
Console.WriteLine("Photo {0} has title {1} with url {2}", photo.PhotoId, photo.Title, photo.WebUrl);
List.Add(new FlickrData(photo));
}
ImageList.ItemsSource = List;
}
}
公共部分类主窗口:窗口
{
公共ObservableCollection列表=新ObservableCollection();
public static Flickr Flickr=新Flickr(“xxxxxxxxxxxx”);
公共主窗口()
{
初始化组件();
}
带有输入的公共无效搜索(对象发送者、路由目标)
{
var options=newphotosearchoptions{Tags=SearchInput.Text,PerPage=20,Page=1};
PhotoCollection photos=flickr.photosearch(选项);
List.Clear();
foreach(照片中的照片)
{
字符串flickrl=photo.WebUrl;
Console.WriteLine(“照片{0}的标题为{1},url为{2}”,Photo.PhotoId,Photo.title,Photo.WebUrl);
添加(新FlickrData(照片));
}
ImageList.ItemsSource=列表;
}
}
执行此操作以清理流程
ListView
更改为ItemsSource=“{Binding List}”
,只需执行一次ImageList.ItemsSource=List代码>
列表控件将相应地更新自身,因为
ObservableCollection
发送列表控件订阅的更改通知。似乎在类型ImageBrush
中找不到ItemsSource
不是针对ImageBrush
而是针对ListView
。