当WPF选项卡控件中的选项卡更改时,是否有办法防止选项卡卸载/重新加载?或者,如果这是不可能的,有没有推荐的方法来缓存标签内容,以便它们不必在每个标签更改时重新生成?WPF TabControl - 防止卸载选项卡更改?
例如,一个选项卡的UI完全可定制并存储在数据库中。当用户选择要处理的对象时,自定义布局中的项目会填充该对象的数据。用户希望在初始加载时或检索数据时稍微延迟一些,但在选项卡之间来回切换时不会出现延迟,并且更改制表符时的延迟非常明显。
当WPF选项卡控件中的选项卡更改时,是否有办法防止选项卡卸载/重新加载?或者,如果这是不可能的,有没有推荐的方法来缓存标签内容,以便它们不必在每个标签更改时重新生成?WPF TabControl - 防止卸载选项卡更改?
例如,一个选项卡的UI完全可定制并存储在数据库中。当用户选择要处理的对象时,自定义布局中的项目会填充该对象的数据。用户希望在初始加载时或检索数据时稍微延迟一些,但在选项卡之间来回切换时不会出现延迟,并且更改制表符时的延迟非常明显。
我在这里找到一个解决办法:https://web.archive.org/web/20120429044747/http://eric.burke.name/dotnetmania/2009/04/26/22.09.28
编辑:这是纠正链接: http://web.archive.org/web/20110825185059/http://eric.burke.name/dotnetmania/2009/04/26/22.09.28
它切换标签的时候基本上存储标签的ContentPresenter和负载,最多而不是重新绘制它。当拖放标签时,它仍然会导致延迟,因为这是一个删除/添加操作,但是有一些修改让我也消失了(以较低的调度程序优先级运行删除代码,然后添加代码,因此添加操作有机会取消删除操作并使用旧ContentPresenter,而不是绘制一个新的)
编辑:上面的链接似乎不再工作,所以我会在这里粘贴代码的副本。它已被修改了一些允许拖放,但它仍然应该以同样的方式工作。
// Extended TabControl which saves the displayed item so you don't get the performance hit of
// unloading and reloading the VisualTree when switching tabs
// Obtained from http://eric.burke.name/dotnetmania/2009/04/26/22.09.28
// and made a some modifications so it reuses a TabItem's ContentPresenter when doing drag/drop operations
[TemplatePart(Name = "PART_ItemsHolder", Type = typeof(Panel))]
public class TabControlEx : System.Windows.Controls.TabControl
{
// Holds all items, but only marks the current tab's item as visible
private Panel _itemsHolder = null;
// Temporaily holds deleted item in case this was a drag/drop operation
private object _deletedObject = null;
public TabControlEx()
: base()
{
// this is necessary so that we get the initial databound selected item
this.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
}
/// <summary>
/// if containers are done, generate the selected item
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (this.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
this.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
UpdateSelectedItem();
}
}
/// <summary>
/// get the ItemsHolder and generate any children
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_itemsHolder = GetTemplateChild("PART_ItemsHolder") as Panel;
UpdateSelectedItem();
}
/// <summary>
/// when the items change we remove any generated panel children and add any new ones as necessary
/// </summary>
/// <param name="e"></param>
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
if (_itemsHolder == null)
{
return;
}
switch (e.Action)
{
case NotifyCollectionChangedAction.Reset:
_itemsHolder.Children.Clear();
if (base.Items.Count > 0)
{
base.SelectedItem = base.Items[0];
UpdateSelectedItem();
}
break;
case NotifyCollectionChangedAction.Add:
case NotifyCollectionChangedAction.Remove:
// Search for recently deleted items caused by a Drag/Drop operation
if (e.NewItems != null && _deletedObject != null)
{
foreach (var item in e.NewItems)
{
if (_deletedObject == item)
{
// If the new item is the same as the recently deleted one (i.e. a drag/drop event)
// then cancel the deletion and reuse the ContentPresenter so it doesn't have to be
// redrawn. We do need to link the presenter to the new item though (using the Tag)
ContentPresenter cp = FindChildContentPresenter(_deletedObject);
if (cp != null)
{
int index = _itemsHolder.Children.IndexOf(cp);
(_itemsHolder.Children[index] as ContentPresenter).Tag =
(item is TabItem) ? item : (this.ItemContainerGenerator.ContainerFromItem(item));
}
_deletedObject = null;
}
}
}
if (e.OldItems != null)
{
foreach (var item in e.OldItems)
{
_deletedObject = item;
// We want to run this at a slightly later priority in case this
// is a drag/drop operation so that we can reuse the template
this.Dispatcher.BeginInvoke(DispatcherPriority.DataBind,
new Action(delegate()
{
if (_deletedObject != null)
{
ContentPresenter cp = FindChildContentPresenter(_deletedObject);
if (cp != null)
{
this._itemsHolder.Children.Remove(cp);
}
}
}
));
}
}
UpdateSelectedItem();
break;
case NotifyCollectionChangedAction.Replace:
throw new NotImplementedException("Replace not implemented yet");
}
}
/// <summary>
/// update the visible child in the ItemsHolder
/// </summary>
/// <param name="e"></param>
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
UpdateSelectedItem();
}
/// <summary>
/// generate a ContentPresenter for the selected item
/// </summary>
void UpdateSelectedItem()
{
if (_itemsHolder == null)
{
return;
}
// generate a ContentPresenter if necessary
TabItem item = GetSelectedTabItem();
if (item != null)
{
CreateChildContentPresenter(item);
}
// show the right child
foreach (ContentPresenter child in _itemsHolder.Children)
{
child.Visibility = ((child.Tag as TabItem).IsSelected) ? Visibility.Visible : Visibility.Collapsed;
}
}
/// <summary>
/// create the child ContentPresenter for the given item (could be data or a TabItem)
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
ContentPresenter CreateChildContentPresenter(object item)
{
if (item == null)
{
return null;
}
ContentPresenter cp = FindChildContentPresenter(item);
if (cp != null)
{
return cp;
}
// the actual child to be added. cp.Tag is a reference to the TabItem
cp = new ContentPresenter();
cp.Content = (item is TabItem) ? (item as TabItem).Content : item;
cp.ContentTemplate = this.SelectedContentTemplate;
cp.ContentTemplateSelector = this.SelectedContentTemplateSelector;
cp.ContentStringFormat = this.SelectedContentStringFormat;
cp.Visibility = Visibility.Collapsed;
cp.Tag = (item is TabItem) ? item : (this.ItemContainerGenerator.ContainerFromItem(item));
_itemsHolder.Children.Add(cp);
return cp;
}
/// <summary>
/// Find the CP for the given object. data could be a TabItem or a piece of data
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
ContentPresenter FindChildContentPresenter(object data)
{
if (data is TabItem)
{
data = (data as TabItem).Content;
}
if (data == null)
{
return null;
}
if (_itemsHolder == null)
{
return null;
}
foreach (ContentPresenter cp in _itemsHolder.Children)
{
if (cp.Content == data)
{
return cp;
}
}
return null;
}
/// <summary>
/// copied from TabControl; wish it were protected in that class instead of private
/// </summary>
/// <returns></returns>
protected TabItem GetSelectedTabItem()
{
object selectedItem = base.SelectedItem;
if (selectedItem == null)
{
return null;
}
if (_deletedObject == selectedItem)
{
}
TabItem item = selectedItem as TabItem;
if (item == null)
{
item = base.ItemContainerGenerator.ContainerFromIndex(base.SelectedIndex) as TabItem;
}
return item;
}
}
我认为StackOverflow只是错误地解析链接的降价。如果您复制/粘贴整个URL(或者不使用\ [\] markdown),它就可以工作。 http://web.archive.org/web/20110825185059/http://eric.burke.name/dotnetmania/2009/04/26/22.09.28 – skst 2016-08-09 17:56:48
我正在尝试使用您的解决方案,因为它似乎解决了我面临的确切问题。但是,我不知道如何完成工作......例如,变量'_itemsHolder'总是'null'。该控件位于我的XAML中,并且所有内容都可以正确显示,但是XAML中是否有特定的参考需要弥补差距? – DonBoitnott 2017-07-14 13:36:53
@DonBoitnott什么时候它是空的?你说它工作正常,所以它必须在某个时候填充。 – Rachel 2017-07-14 15:03:25
我想补充这一点,我有一个类似的问题和管理的缓存是代表一个标签项的内容在后面的代码一个用户控件来解决这个问题。
在我的项目中,我有一个绑定到集合(MVVM)的选项卡控件。但是,第一个选项卡是概览,其中显示了列表视图中所有其他选项卡的摘要。我遇到的问题是,无论何时用户将其选择从项目选项卡移至概览选项卡,总览都会重新绘制所有摘要数据,根据集合中的项目数量,可能需要10-15秒。 (注意它们不是从数据库或其他任何地方重新加载实际数据,它完全是花费时间的摘要视图的绘图)。
我想要的是,只有当数据上下文第一次加载并且任何后续标签之间的切换瞬间完成时,这个加载汇总视图才会发生。
解决方案:
类涉及: MainWindow.xaml - 包含选项卡控件的主要页面。 MainWindow.xaml.cs - 代码隐藏在上面。 MainWindowViewModel.cs - 查看以上视图的模型,包含集合。 Overview.xaml - 绘制概览标签项目内容的用户控件。 OverviewViewModel.cs - 查看上述视图的模型。
步骤:
更换的DataTemplate在“主窗口。XAML”与名为空的用户控件绘制概述选项卡项目‘OverviewPlaceholder’
做参考,以'内公众MainWindowViewModel.cs“
添加静态参考‘中概述’OverviewViewModel 'MainWindow.xaml.cs'
将事件处理程序添加到用户控件'OverviewPlaceholder'的已加载事件中,在此方法内实例化静态引用到'Overview',仅当它为null时,设置此引用的datacontext到当前datacontext(即'MainWindowViewModel')内的'OverviewViewModel'引用并设置占位符的内容成为“概述”的静态引用。
现在,因为每个被加载时间(即用户点击到概述选项卡)概述页面只绘制一次,它把已经呈现,静态用户控制回到页面上。
我有一个非常简单的解决方案,以避免选项卡更改选项卡更改, 在tabItem而不是content属性中使用contentPresenter。
例如(在MVVM风格)
更换
<TabItem Header="Tab1" Content="{Binding Tab1ViewModel}" />
通过
<TabItem Header="Tab1"> <ContentPresenter Content="{Binding Tab1ViewModel}" /> </TabItem>
为什么这个答案是upvoted? 'ContentPresenter'将在包含其内容的标签切换时被卸载。这什么都不做。 – Sinatr 2016-02-23 08:54:57
只要选择我不认为的TabItems卸载/再项目在选项卡控制通道安格斯。我不确定,但也许你的TabControl的SelectionChanged逻辑需要改变,以便它不会每次都重新查询数据库? – ASanch 2010-08-30 14:28:02
每次更改选项卡(我使用MVVM设计模式)时,都会运行DataTemplates的加载/卸载事件 – Rachel 2010-08-30 14:56:33
因此,在您的应用程序中,只要选定选项卡更改,就会触发与数据库的连接以检索对象的数据? – ASanch 2010-08-30 16:04:17