在C#WPF中动态创建窗口/控件 [英] Dynamic window/control creation in c# wpf

查看:84
本文介绍了在C#WPF中动态创建窗口/控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我是一个初学者,对应用程序设计的理解非常基础.我刚刚开始学习WPF,并且对Windows/图形编程还很陌生.

我正在尝试创建一个应该至少包含三个窗口的wpf项目.第一个窗口应使用文本框获取汽车数量"的输入.当用户单击提交"按钮时,将出现另一个带有大量文本框的窗口,以收集有关汽车详细信息的输入,例如名称,型号,最高时速,年份等.

假设如果用户输入为"2",则第二个窗口应首先收集car1的详细信息,然后在用户单击提交卡片"按钮后,同样应收集car2的详细信息.一旦用户填写了所有汽车的详细信息并单击提交,就会出现第三个窗口,它将执行应用程序逻辑,如汽车比较,最佳选择等,并显示给用户.

我可以在单独的窗口中创建文本框和其他控件,我认为我可以使用"newwindow.show();".在运行时打开新窗口的方法?但是,对我来说,主要的挑战是"window2"应根据用户在"window1"中给出的选择工作.

您能解释一下如何创建一个这样的应用程序吗?我脑子里有很多想法来创建许多不同的计算机应用程序,但是我已经有一份工作,而且我只是出于业余爱好学习计算机编程,因此在经济上负担不起参加它的正式课程.因此,请帮助我提高编程技能;)

如果您能提供语法,示例或图形插图来解释您的概念,以便像我这样的沼泽标准业余爱好者可以轻松理解,我将不胜感激.

干杯..

Hi. I am a beginner and my understanding of application design is very basic. I have just started to learn WPF and I am quite new to windows/graphics programming.

I am trying to create a wpf project that should contain at least three windows. The first window should get the input for "number of cars" using a text box. when the user clicks the ''submit'' button, another window should appear with a bunch of textboxes to collect input for car details like name, model, top speed, year, etc.

Suppose if the user has given the input as ''2'' the second window should collect the details of car1 first and after user clicks ''submit cardetails'' button, the same should collect the details of car2. Once the user fills up the details of all the cars and clicks submit, a third window should appear which will perform the application logic like car-comparison, best of choice, etc and display it to user.

I can create text boxes and other controls in seperate windows and i think i can use "newwindow.show();" method to open a new window during run time ? But, the major challenge for me is that the ''window2'' should work according to the choice given by the user in ''window1''.

Could you please explain me how to create one such application ? I have so many ideas in my head to create many different computer applications, but I already have a job and I learn computer programming only as a hobby and can''t financially afford to take formal courses on it. So, please help me to improve my skills in programming ;)

I would greatly appreciate it if you could please provide syntax, examples or graphical illustrations to explain your concepts so that a bog standard amateur like me can easily understand.

Cheers..

推荐答案

您想要的是向导表单. 扩展的WPF工具包 [
What you want is a wizard form. The Extended WPF Toolkit [^] provides just such an item.


您应该考虑编写一个类,该类存储汽车的某种任意细节:

You should consider writing a class that stores some kind of arbitrary detail for a car:

class CarDetail
{
   public string DetailName { get; set; }
   public string DetailValue { get; set; }
}



然后为要收集的每个详细信息创建一个CarDetail对象.如果将此集合存储在ViewModel中,则可以将项目控件绑定到它-并使项目控件显示一个文本框:)



Then create a CarDetail object for each detail you want to collect. If you store this collection in a ViewModel, you can then bind an items control to it - and have the items control show a textbox :)

<itemscontrol itemssource="{Binding Details}">
 <itemscontrol.itemtemplate>
  <datatemplate>
   <stackpanel orientation="Horizontal">
    <textblock text="{Binding DetailName}" />
    <textbox text="{Binding DetailValue}" />
   </stackpanel>
  </datatemplate>
 </itemscontrol.itemtemplate>
</itemscontrol>



因此,您在这里所说的是我们有一组任意的细节-以及如何呈现每个细节?作为标签的详细信息名称,以及用于获取详细信息的文本框.

然后,您可以稍后对其进行扩展,以便如果您想收集其他类型的详细信息,则可以使用其他数据模板-一个用于组合框,一个用于复选框等.

为此,您需要确保这些类实现了INotifyPropertyChanged,请在以下位置继续阅读:

http://www.codeproject.com/KB/WPF/wpfstepbystep1.aspx [ ^ ]

其中说明了如何执行操作或使用MVVM库,例如 Cinch V2:我的Cinch MVVM框架的版本2:第1部分n之1 [ ^ ]或甚至我自己的一个非常简单的 Apex简介 [



So what you are doing here is saying that we have a set of some arbitrary details - and how do we present each one? As a label the name of the detail, alongside a textbox to get the detail.

You can then later extend it so that if you want to collect other types of details you can use other datatemplates - one for a combo box, one for a checkbox etc.

To do this in this way you''ll need to make sure that that classes implement INotifyPropertyChanged, read up on this at:

http://www.codeproject.com/KB/WPF/wpfstepbystep1.aspx[^]

which explains how to do that, or use a MVVM library such as Cinch V2: Version 2 of my Cinch MVVM framework: Part 1 of n[^] or even my own very simple one Introducing Apex[^]

Good luck!


你好转向者,

感谢您的简短介绍.不用担心,这里有很多帮助可以帮助您入门.表现出主动性,尝试一些事情,阅读博客,下载并尝试使用示例,您将大步前进.

如果您使用简短的代码示例来提问,这些示例说明了您的问题并表明您已尝试解决问题,那么这里有很多专家可以帮助您解决问题.

我已经为您提供了另一个非常相似的问题的阅读材料,因此,我将再给您提供一条建议:确保您不重新发布问题.如果您有什么要澄清的地方,请回到原始问题,然后使用改善问题"功能.

就设计而言,如果应用程序用于创建汽车列表,我希望在用户界面中看到以下内容:
1)在摘要视图中的侧边栏,其中列出了现有的汽车
2)我可以添加新车的主要区域
3)一种打开第二个窗口/页面以更详细地查看特定汽车的方式(可能需要编辑)

希望这对您有帮助.

干杯.
Hello steersteer,

Thanks for your humble introduction. Don''t worry, there''s lots of help here to get you started. Show initiative, try things out, read blogs, download and try the samples and you''ll get far.

If you ask questions with short code samples that illustrate your problems and show that you''ve tried to figure things out, you have a whole host of experts here that will help you work through things.

I''ve given you some reading on your other question that is quite similar, so I''ll give you one more piece of advice: make sure you''re not reposting questions. If you have something to clarify, go back to your original question and use the "Improve Question" feature.

As far as design goes, here''s what I would expect to see in the UI as a user if the application was for creating a list of cars:
1) A side bar with a list of existing cars, in a summary view
2) A main area where I can add new cars
3) A way to open a second window/page to see a particular car in more detail (possibly edit)

Hope this helps you along the way.

Cheers.


这篇关于在C#WPF中动态创建窗口/控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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