图像模板的 WPF 容器

发布于 2024-08-24 02:07:21 字数 1052 浏览 3 评论 0原文

我将图像的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-08-31 02:07:22
<ListBox ItemsSource="{Binding Library}">
  <ListBox.ItemTemplate>
    <DataTemplate>    
         <Image Source="{Binding Path=url}" />
     </DataTemplate>
  </ListBox.ItemTemplate>
 </ListBox>

ListBox 的 DataContext 应该是您的 DataSet。您可以使用 OnLoad 而不是 OnInit

无论如何,我不推荐 DataSet 绑定,如果您为 Library 创建 ViewModel 类并创建 Library 实体的集合,它会更易于管理

<ListBox ItemsSource="{Binding Library}">
  <ListBox.ItemTemplate>
    <DataTemplate>    
         <Image Source="{Binding Path=url}" />
     </DataTemplate>
  </ListBox.ItemTemplate>
 </ListBox>

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

戏剧牡丹亭 2024-08-31 02:07:21

您可以轻松地在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文