DataBinding 在 WPF 中动态创建 jpeg
在我的 WPF 应用程序中,我动态创建 jpeg 文件。它们被保存在 bin/Debug 或 bin/Release 中。
我有一个数据绑定 WrapPanel
,其中包含一个使用 ValueConverter
的图像控件,如下所示:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
}
value 的示例值为 Images\400\26.jpg
我假设默认情况下在 bin\Debug 或 bin\Release 中查找。
我的问题是我似乎无法将图像控件数据绑定到动态创建的 jpeg。但我可以将数据绑定到我标记为 Include
且其 BuildAction
为 Content
的其他 jpeg。
如何将数据绑定到编译时不存在的动态创建的图像?
In my WPF application I am creating jpeg files dynamically. They are being saved in bin/Debug or bin/Release.
I have a databound WrapPanel
with an image control that is using a ValueConverter
like so:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
}
An example value for value is Images\400\26.jpg
which I assume is by default looking in bin\Debug or bin\Release.
My problem is that I can't seem to be able to databind image controls to the dynamically created jpegs. But I can databind to other jpegs that I've marked as Include
and whose BuildAction
is Content
.
How do I databind to the dynamically created images that aren't present at compile time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建图像路径集合,并向其中添加每个新创建的图像的路径。现在您可以将您的包装面板绑定到该集合。
您可以在 ViewModel 类中实现此集合,然后使此 ViewModel 类成为您的 DataContext,并使用转换器将您的 wrapPanel 绑定到该集合。
编辑:这是一个示例文件:
MainWindowViewModel.cs:
MainWindow.xaml.cs:
MainWindow.xaml:
如您所见,在视图模型中我们定义了图像Uris的集合。
在后面的主窗口代码中,我们将该视图模型的对象设置为窗口数据上下文,并在 xaml 中将 listView(或您选择的任何其他控件)的 Item 源设置为绑定属性 ImagesList,在我们的数据中上下文即视图模型。
最后,我们可以修改您的动态 jpeg 创建器函数:
此时您将获得一个列表视图,显示动态创建的图像列表
You can create a collection of Images Paths, and add to it every newly created image's path. now you can bind your wrappanel to this collection.
You can implement this collection in ViewModel class, and then make this ViewModel class be your DataContext, and bind your wrapPanel to that collection using your converter.
Edit: Here is a sample files:
MainWindowViewModel.cs:
MainWindow.xaml.cs:
MainWindow.xaml:
As you can see, in the view model we defined a collection of the Uris of images.
In the main window code behind we set an object of that view model as our window data context, and in the xaml we set the Item source of our listView (or any other control you choose) to binding of the property ImagesList, at our data context which is the view model.
Finally, we can modify your dynamically jpeg creator function:
At this point you'll get a listview displays list of dynamically created images