[英]Re-Using Datagrid In Multiple TabItems
I have a WPF application that uses a TabControl. 我有一个使用TabControl的WPF应用程序。 Each TabItem will contain a datagrid.
每个TabItem将包含一个数据网格。 When the application starts up, there is one TabItem that loads by default that displays an "Accounts" datagrid.
当应用程序启动时,默认情况下会加载一个TabItem,其中显示一个“ Accounts”数据网格。 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.
它与用于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?
如何定义可在每个新添加的TabItem中使用但与第一个TabItem上的原始datagrid不同的datagrid?
So if I understand correctly what you want is a default DataGrid
for the first tabitem
and then for each new tabitem
same datagrid
. 因此,如果我正确理解,您想要的是第一个
tabitem
的默认DataGrid
,然后对于每个新tabitem
相同datagrid
。
Problem here is that a single DataGrid
cannot be part of Two TabItems
at the same time. 这里的问题是单个
DataGrid
不能同时属于两个TabItems
。 So what you would have to do it first declare a DataGrid
in a scope where it can be accessed in the code behind. 因此,您首先必须在可以在后面的代码中访问它的范围内声明一个
DataGrid
。 Next, when user adds a new tabitem
, first time, add a tab item
dynamically in the tab control
and set content
equal to DataGrid
. 接下来,当用户第一次添加新的
tabitem
时,请在tab control
动态添加tab item
,并将content
设置为等于DataGrid
。 When user clicks the add new tab item again, remove the content
of tab item which previously had DataGrid
and then Add datagrid
in the new tab item
. 当用户再次单击添加新选项卡项时,请删除先前具有
DataGrid
的选项卡项的content
,然后在新tab item
添加datagrid
。 You will also have to handle the selection change
event of tabs and inside that event you will have to remove DataGrid
from last selected item and put in the newly selected item. 您还必须处理选项卡的
selection change
事件,并且在该事件内,您必须从最后选择的项目中删除DataGrid
并将其放置在新选择的项目中。
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 我不确定您是否确实需要为不同的选项卡项目使用相同的dataGrid,但是在实现此方法之前先考虑一下其他可能的解决方案
In this case, I'd recommend using the MVVM pattern. 在这种情况下,我建议使用MVVM模式。
Have your main ViewModel define an public ObservableCollection<object> Items
property. 让您的主要ViewModel定义一个
public ObservableCollection<object> Items
属性。 Bind your TabControl
's ItemsSource
to the Items
. 将
TabControl
的ItemsSource
绑定到Items
。
Define a DataTemplate
for an AccountsViewModel
which contains the DataGrid
which should be displayed on the Accounts TabItem
. 为
AccountsViewModel
定义一个DataTemplate
,该AccountsViewModel
包含应显示在Accounts TabItem
上的DataGrid
。
Define a DataTemplate
for a TransactionsViewModel
which contains the DataGrid
which should be displayed on each Transactions TabItem
. 为
TransactionsViewModel
定义一个DataTemplate
,其中包含应该在每个 Transactions TabItem
上显示的DataGrid
。
On the Account Tab
add in XAML
a DataGrid
, let's say AccountDataGrid 在“
Account Tab
在XAML
添加一个DataGrid
,比如说AccountDataGrid
For other types, as they generated at runtime, it's better to manage them from the code. 对于其他类型,由于它们是在运行时生成的,因此最好通过代码进行管理。
Create DataGrid
object that will be shared by other TabItems
, let's say SharedDataGrid 创建
DataGrid
,将其他共享对象TabItems
,让我们说SharedDataGrid
After you can do something like this, for example: 在您可以执行以下操作之后,例如:
Define your custom TabItem
class 定义您的自定义
TabItem
类
public sealed class CustomTabItem : TabItem
{
}
and after override inside that class 然后在该类中重写
protected override void OnInitialized(EventArgs e)
{
//assign shared SharedDataGrid to the content of TabItem
}
In that method actually assign SharedDataGrid to the content of the just created and initialized TabItem
. 在该方法中,实际上将SharedDataGrid分配给刚创建和初始化的
TabItem
。
Should work. 应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.