图像模板的 WPF 容器
我将图像的 url 作为字符串存储在 sql ce 3.5 数据库中。我想检索网址并将其显示在主应用程序窗口中。这是代码:
DataSet myDataSet;
private void OnInit(object sender, EventArgs e)
{
string connString = Properties.Settings.Default.SystemicsAnalystDBConnectionString;
OleDbConnection conn = new OleDbConnection(connString);
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT url FROM Library;", conn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "Library");
myListBox.DataContext = myDataSet;
}
第一个问题是我认为 onInit 方法没有被触发。但我不知道其中的原因。
第二个问题是 XAML 文件。我需要一个图像容器(如文本框的列表框),因为我不知道有多少图像,所以我需要某种模板:
<DataTemplate>
<StackPanel>
<Image Source="{Binding Path=url}" />
</StackPanel>
</DataTemplate>
但是必须有某种容器将数据上下文设置为数据源。
有人可以帮忙吗?
I'm storing the urls to the images in a sql ce 3.5 database as strings. I want to retrieve the urls and display them in the main application window. Here is the code:
DataSet myDataSet;
private void OnInit(object sender, EventArgs e)
{
string connString = Properties.Settings.Default.SystemicsAnalystDBConnectionString;
OleDbConnection conn = new OleDbConnection(connString);
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT url FROM Library;", conn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "Library");
myListBox.DataContext = myDataSet;
}
The first problem is that I don't think the method onInit is fired. But I don't know the reason for that.
The second problem is with XAML file. I need a container for images (like the listbox for textboxes) and since I won't know how many images are there I need some kind of a template:
<DataTemplate>
<StackPanel>
<Image Source="{Binding Path=url}" />
</StackPanel>
</DataTemplate>
But there has to be some kind of a container that would have the datacontext set to the data source.
Could anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ListBox 的 DataContext 应该是您的 DataSet。您可以使用 OnLoad 而不是 OnInit
无论如何,我不推荐 DataSet 绑定,如果您为 Library 创建 ViewModel 类并创建 Library 实体的集合,它会更易于管理
The DataContext for the ListBox should be your DataSet. You can use OnLoad instead of OnInit
Anyway I dont recommend the DataSet binding, it would be more managable if you create ViewModel class for your Library and create a collection of Library entities
您可以轻松地在 wpf 中自定义列表框以在其中包含图像而不是文本。使用 ItemTemplate,或者如果您想更改控件本身,则使用 ControlTemplate。
You can customize a listbox in wpf quite easily to have images in it, instead of text. Use the ItemTemplate or if you want to change to control itself, the ControlTemplate.