在多个 TabItem 中重用 Datagrid
我有一个使用 TabControl 的 WPF 应用程序。每个 TabItem 将包含一个数据网格。当应用程序启动时,默认情况下会加载一个显示“帐户”数据网格的 TabItem。该数据网格仅显示帐户信息。然后用户可以选择添加新选项卡。对于添加的每个选项卡,我需要加载相同的数据网格。它与用于帐户选项卡项的数据网格不同。新的数据网格将用于输入交易。如何定义一个可以在每个新添加的 TabItem 中使用的数据网格,但与第一个 TabItem 上的原始数据网格不同?
I have a WPF application that uses a TabControl. Each TabItem will contain a datagrid. When the application starts up, there is one TabItem that loads by default that displays an "Accounts" datagrid. This datagrid displays only account information. The user can then choose to add new Tabs. For each tab that is added, I need the same datagrid to be loaded. It is NOT the same datagrid that is used for the Accounts TabItem. The new datagrid will be used to enter transactions. How can I define a datagrid that I can use in each newly added TabItem, but is different than the original datagrid on the first TabItem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,如果我理解正确的话,您想要的是第一个
tabitem
的默认DataGrid
,然后为每个新的tabitem
相同的datagrid.
这里的问题是单个
DataGrid
不能同时成为两个TabItems
的一部分。因此,您首先必须在可以在后面的代码中访问它的范围内声明一个DataGrid
。接下来,当用户添加新的tabitem
时,第一次在tab 控件
中动态添加tabitem
并设置content 等于
DataGrid
。当用户再次单击添加新选项卡项时,删除以前具有DataGrid
的选项卡项的content
,然后在新的中添加
。您还必须处理选项卡的datagrid
>选项卡项选择更改
事件,并且在该事件中,您必须从最后选定的项目中删除DataGrid
并放入新选定的项目。我不确定您是否真的需要相同的 dataGrid 来处理不同的选项卡项,但在实施此方法之前请考虑其他可能的解决方案
So if I understand correctly what you want is a default
DataGrid
for the firsttabitem
and then for each newtabitem
samedatagrid
.Problem here is that a single
DataGrid
cannot be part of TwoTabItems
at the same time. So what you would have to do it first declare aDataGrid
in a scope where it can be accessed in the code behind. Next, when user adds a newtabitem
, first time, add atab item
dynamically in thetab control
and setcontent
equal toDataGrid
. When user clicks the add new tab item again, remove thecontent
of tab item which previously hadDataGrid
and then Adddatagrid
in the newtab item
. You will also have to handle theselection change
event of tabs and inside that event you will have to removeDataGrid
from last selected item and put in the newly selected item.I am not sure you really need this same dataGrid for different tab items thing or not but think before implementing this approach about other possible solutions
在这种情况下,我建议使用 MVVM 模式。
让您的主 ViewModel 定义一个 public ObservableCollection
为
AccountsViewModel
定义一个DataTemplate
,其中包含应显示在 AccountsTabItem
上的DataGrid
。为
TransactionsViewModel
定义一个DataTemplate
,其中包含应显示在每个事务TabItem< 上的
DataGrid
/代码>。In this case, I'd recommend using the MVVM pattern.
Have your main ViewModel define an
public ObservableCollection<object> Items
property. Bind yourTabControl
'sItemsSource
to theItems
.Define a
DataTemplate
for anAccountsViewModel
which contains theDataGrid
which should be displayed on the AccountsTabItem
.Define a
DataTemplate
for aTransactionsViewModel
which contains theDataGrid
which should be displayed on each TransactionsTabItem
.在
Account Tab
中添加XAML
一个DataGrid
,例如 AccountDataGrid对于其他类型,因为它们是在运行时生成的,最好从代码中管理它们。
创建将由其他
TabItems
共享的DataGrid
对象,比方说 SharedDataGrid在您可以执行类似的操作后,例如:
定义您的自定义 < code>TabItem 类
以及在该类内重写之后
在该方法中,实际上将 SharedDataGrid 分配给刚刚创建和初始化的
TabItem
的内容。应该可以工作。
On the
Account Tab
add inXAML
aDataGrid
, let's say AccountDataGridFor other types, as they generated at runtime, it's better to manage them from the code.
Create
DataGrid
object that will be shared by otherTabItems
, let's say SharedDataGridAfter you can do something like this, for example:
Define your custom
TabItem
classand after override inside that class
In that method actually assign SharedDataGrid to the content of the just created and initialized
TabItem
.Should work.