需要帮助在WPF中动态创建列表框 [英] Need Help Dynamically Creating ListBoxes in WPF

查看:64
本文介绍了需要帮助在WPF中动态创建列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我搜索了过去几天,试图找到答案. :(

Hello everyone!

I have searched for the past couple of days trying to find the answer to this. :(

List<SomeClass> myLists = new List<SomeClass>();

class SomeClass
{
    public string Name{get;set;}
    public List<string> StringList {get;set;}
}



因此,以前面的代码为例,wpf中是否有一种方法可以使用myLists在具有水平方向的StackPanel之类的东西中动态创建ListBoxes,然后将相应的StringList绑定到ItemsSource属性以填充项目在列表框中?

我遇到的主要问题是找到一种动态创建ListBoxes的方法.我一直在使用数据模板来尝试使其工作,但是我对数据模板是陌生的,我可能会丢失一些东西.



So, using the previous code as an example, is there a way in wpf to use myLists to dynamically create ListBoxes in something like a StackPanel with a horizonal orientation and then bind the corresponding StringList to the ItemsSource property to populate the items in the ListBox?

The main problem I''m having is just finding a way to dynamically create the ListBoxes. I''ve been working with datatemplates to try to get this to work, but I''m new to datatemplates and I''m probably missing something.

Thank you in advance for the help!

推荐答案

您可以在代码中添加所需的任何内容.

我通常建议一种解决此类问题的非常通用的方法.如果您不知道如何放置一些控件,创建一些布局等,请尝试首先使用设计器.测试应用程序以查看结果.如果这是您想要的,请找到自动生成的代码并了解其工作方式.

使用WPF,查找那些自动生成的文件会有些棘手,因为它们没有显示在解决方案资源管理器"树形视图中,而是作为临时文件创建的.您可以使用某种文件搜索工具(不是VS中的一种)来找到它们.通常,它们放置在相对于您的项目的"obj \ Debug"或"obj \ Relese"子目录中(更确切地说,是"obj \
You can add anything you want in the code.

I usually advise a very general method of solving such problems. If you don''t know how to put some control, create some layout, etc., try to do it using the designer first. Test the application to see the results. If this is what you want, locate the auto-generated code and learn how it works.

With WPF, it''s a bit trickier to find those auto-generated files, because they are not shown in your Solution Explorer tree view but are created as temporary file. You can use some file search tool (not the one of VS) to find them. Normally, they are put in "obj\Debug" or "obj\Relese" sub-directories relative to your project (more exactly, "obj\


(configuration_name)").

在最简单的情况下,添加控件如下所示:
(configuration_name)").

In the simplest case, adding a control looks like this:
ListBox lb = new ListBox();
this.parentPanel.Children.Add(lb);


上面,我假设您有一些要从System.Windows.Controls.Panel派生的类型的面板,您想用作其他控件的父面板.请参阅每个控件上的MSDN帮助,以了解应使用哪些属性来设置所需的布局和其他功能.同样,查看自动生成的代码可以很好地了解所涉及的内容.

您将需要使用在自动生成的代码中发现的完全相同的技术.仅不要使用变量的丑陋命名方式以及设计人员通常使用的方法-这样的名称违反了(好的)Microsoft命名约定.给所有语义上合理的名称,不要使用数字字符和其他自动"污垢.

祝你好运,

-SA


Above, I assumed that you have some panel of the type derived from System.Windows.Controls.Panel you want to use as a parent for other control. See MSDN help on each control to find out what properties should you use to set up desired layout and other features. Again, looking at the auto-generated code is good to see what''s involved.

You will need to use exact same techniques you find in your auto-generated code. Only don''t use ugly naming of the variables and method designers usually use — such names violate (good) Microsoft naming conventions. Give everything semantically sensible names, without numeric characters and other "automated" dirt.

Good luck,

—SA


您始终可以在DataTemplate中使用ListBox ListBox (请参阅如何创建DataTemplate http://social.msdn.microsoft.com/Forums/en -US/wpf/thread/760eab7e-63c0-45ac-b7fc-167e754354ce [
You can always have a ListBox with a ListBox in the DataTemplate (see to see how to create a DataTemplate http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/760eab7e-63c0-45ac-b7fc-167e754354ce[^]). You can put the Items in a different container by using ItemsPanelTemplate:

<ItemsPanelTemplate>
  <StackPanel />
</ItemsPanelTemplate>



注意:要使此工作有效,您必须有一个IEnumerable列表,其中包含另一个IEnuemrable.



Note: to make this work, you have to have an IEnumerable list that contains another IEnuemrable.


这篇关于需要帮助在WPF中动态创建列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆