WPF 选项卡控件,所有选项卡具有相同的控件
如何使用 wpf 中的数据控件创建 n 个选项卡?
假设主应用程序有一个名为“新客户”和“保存数据”的按钮。当按下“新客户”时,会出现一个新选项卡,选项卡中包含两个文本框“姓名”和“客户编号”,依此类推。填充两个字段后,按“保存数据”应将焦点选项卡中的信息存储到数据库中。
我遇到的问题是“名称”和“客户编号”文本框的静态名称为 x:Name="CustomerName" 和 x:Name="CustomerNumber"。我发现你不能复制这些。
有人可以建议我如何解决这个问题吗?先感谢您!
How do I create n-numbers of tabs with the data controls in wpf?
Let's say the main application has a button called "new customer" and "save data." When "new customer" is pressed a new tab appears with two text boxes "Name" and "Customer Number" contained in the tab, and so on. Once the two fields are populated, pressing the "save Data" should store the information from the focused tab into a database.
The problem I have is that I have a static name for the "Name" and "Customer Number" text boxes as x:Name="CustomerName" and x:Name="CustomerNumber". I found that you cannot duplicate these.
Can someone advice on how I can tackle this problem? Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 MVVM:
为新客户提供一个视图和一个视图模型,视图上有一个保存按钮,该按钮执行代码将客户保存到数据库中。此代码应该存在于客户模型或客户服务中。
NewCustomerView 不需要与其控件关联的名称,因为您将把它们的 Text 属性绑定到 ViewModel 或基础模型上的属性,除非您必须出于某种其他目的而不是从每个控件获取数据而引用它们。
在主窗体上,您将拥有选项卡控件,其项目源设置为 MainWindowViewModel 上的 ObservableCollection。按“新客户”按钮将在该 ObservableCollection 中填充一个新条目。
在 MainWindowView 中,您可以放置一个将 NewCustomerViewModel 映射到 NewCustomerView 的数据模板,例如:
这基本上是说,当内容是 NewCustomerViewModel 时,呈现 NewCustomerView。
您应该去阅读 MVVM,因为这将解决您的命名问题,以及更好的架构应用程序。
请参阅 Josh Smith 关于 MVVM 的文章,还有很多其他信息也在那里读书。
Using MVVM:
Have a View and a ViewModel for a New Customer, with a save button on the view, which executes code to save a Customer to the database. This code should live in a Customer model or Customer Service.
The NewCustomerView would not need to have names associated with its controls as you will be binding their Text properties to the properties on the ViewModel or an underlying model, unless you have to reference them for some other purpose than getting the data from each one.
On your main form you would have the Tab Control, with its item source set to a ObservableCollection on the MainWindowViewModel. Pressing the New Customer button would populate a new entry in to this ObservableCollection.
In the MainWindowView you would put a data template which maps the NewCustomerViewModel to the NewCustomerView, such as:
This is basically saying when the content is a NewCustomerViewModel, render a NewCustomerView.
You should go and read up on MVVM as this will solve your naming problem, as well a better architected application.
See Josh Smiths article on MVVM, there is lots of other information out there to read too.