以xaml表示的WPF可观测收集
我在用户控件的代码隐藏中创建了一个ObservableCollection。它是在窗口加载时创建的:以xaml表示的WPF可观测收集,wpf,observablecollection,Wpf,Observablecollection,我在用户控件的代码隐藏中创建了一个ObservableCollection。它是在窗口加载时创建的: private void UserControl_Loaded(object sender, RoutedEventArgs e) { Entities db = new Entities(); ObservableCollection<Image> _imageCollection = new ObservableCol
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Entities db = new Entities();
ObservableCollection<Image> _imageCollection =
new ObservableCollection<Image>();
IEnumerable<library> libraryQuery =
from c in db.ElectricalLibraries
select c;
foreach (ElectricalLibrary c in libraryQuery)
{
Image finalImage = new Image();
finalImage.Width = 80;
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(c.url);
logo.EndInit();
finalImage.Source = logo;
_imageCollection.Add(finalImage);
}
}
private void UserControl\u已加载(对象发送方,RoutedEventArgs e)
{
实体db=新实体();
可观察采集(imageCollection)=
新的可观察集合();
IEnumerable libraryQuery=
从db.ElectricalLibraries中的c开始
选择c;
foreach(图书馆查询中的电子图书馆c)
{
图像最终图像=新图像();
最终尺寸。宽度=80;
BitmapImage徽标=新的BitmapImage();
logo.BeginInit();
logo.UriSource=新Uri(c.url);
logo.EndInit();
finalImage.Source=徽标;
_imageCollection.Add(最终图像);
}
}
我需要获取基于保存在数据库中的url创建的图像的ObservableCollection。但我需要ListView或其他ItemsControl在XAML文件中绑定到它,如下所示:
class ViewModel : INotifyPropertyChanged {
public ObservableCollection Images { get { ... } }
...
}
但我不知道如何将ObservableCollection传递给该控件的ItemsSource。我试图创建一个类,然后在xaml文件中创建一个类的实例,但没有成功。我是否应该以某种方式创建静态资源>
任何帮助都将不胜感激。首先,ObservableCollection是一个局部变量。您需要做的是将其作为私有全局变量,并使用公共属性公开它。您可以使用INotifyPropertyChanged接口在实际集合本身发生更改时自动更新图像数据 在XAML中,然后需要将DataContext设置为self,然后可以直接将公共属性绑定到ItemsSource。您可能希望使用ItemTemplate以自定义方式显示项目 干杯, 亚当 请举例说明: 在C#中:
公共MyWindowClass
{
公共可观测集合MyImageCollection
{
收到
设置
}
}
在XAML中:
<UserControl
...
DataContext="{Binding RelativeSource={RelativeSource Self}}">
...
<ListBox ItemsSource="{Binding MyImageCollection}" ItemTemplate="*yourtemplateresource*" />
...
</UserControl>
...
...
现在,我提到使用INotifyPropertyChanged的原因是,如果您尝试:
MyImageCollection = new ObservableCollection<image>();
MyImageCollection=新的ObservableCollection();
列表框中的项目不会自动更新。但是,对于ObservableCollection,您不需要实现INotifyPropertyChanged来基本添加和删除列表项。您必须将
UserControl
的DataContext
设置为您的集合:
DataContext = _imageCollection
您可以在UserControl\u Loaded()
方法中执行此操作
接下来,您需要在XAML中绑定列表视图
的项资源
:
<ListView ItemsSource="{Binding}"/>
将此类用于DataContext
:
DataContext = new ViewModel();
并替换绑定以绑定到图像
属性:
<ListView ItemsSource="{Binding Images}"/>
并将其绑定到控件:
<TextBlock Text="{Binding Message}"/>
当
视图模型
中的消息
属性发生更改时,请记住触发属性更改
事件。当视图模型属性被代码更改时,这将更新UI。技术上正确,但您可能需要添加更多详细信息,并修复一个小问题-ObservableCollection已实现INotifyCollectionChanged和INotifyPropertyChanged,因此无需在您提到的公共属性上重新实现/调用它们。感谢您的帮助。你能提供一些小的代码作为例子吗?@slugster-很抱歉我的回答不清楚。我提到INotifyPropertyChanged的原因纯粹是为了在重新初始化作者自己的公共属性时更新列表。我可能不应该添加多余的信息@三叶草-添加示例。。。
class ViewModel : INotifyPropertyChanged {
public ObservableCollection Images { get { ... } }
public String Message { get { ... } set { ... } }
...
}
<TextBlock Text="{Binding Message}"/>