- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将选项卡绑定(bind)到 ViewModel 中的可观察集合属性。 DataTemplate
每个 View 模型都是一个包含内部选项卡控件的用户控件。当我在内部选项卡控件中切换选项卡,然后在外部选项卡控件中切换选项卡时,新选择的外部选项卡的内容显示选择了相同的内部选项卡,而不是选择了保留选项卡。我尝试使用 x:Shared="False"并确保我的 Equals
和 GetHashCode
View 模型上的方法已正确实现。我还没有看到任何人能够以 MVVM 的方式回答这个问题。
最佳答案
默认情况下,WPF 回收 TabItem
s 切换标签时。这意味着如果 TabItem
的模板是一样的,它会重新使用已经显示的项目并简单地改变它的DataContext
如果你想保持控制状态,比如选中的项目,你需要将它们绑定(bind)到 DataContext
中的某个东西上。 .
所以你的TabItemViewModel
可能具有 SelectedTabIndex
的属性这将绑定(bind)到内部 TabControl 的 SelectedIndex
您会注意到 ListBox、TextBox、CheckBox 等具有相同的行为。由于 TabItem 正在被回收,因此不会丢弃任何控件,因此不会重置它们的值。唯一改变的是 DataContext。
编辑
有覆盖 TabControl
的替代方法类来改变它处理 TabItems 的方式。我过去使用过这个,因为如果 TabItem 的模板发生更改,它会丢弃整个 TabItem 并重新绘制它,这可能会导致性能下降。
发布了一些代码here这将改变这种行为,但该网站似乎已关闭。这是我使用的代码的副本,尽管我已经更改了一些版本。
// 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://www.pluralsight-training.net/community/blogs/eburke/archive/2009/04/30/keeping-the-wpf-tab-control-from-destroying-its-children.aspx
// 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;
}
}
关于wpf - TabItem 内的 TabControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8592802/
我正在构建一个 LOB 应用程序,它有一个主要部分和一个包含各种 TabItems 的 TabControl。点击保存的想法是突出显示任何错误的字段,并且错误的第一个字段获得焦点。 如果第一个也是唯一
我将 xaml 中 tabitem 的背景颜色设置为红色,但是当我运行它并将鼠标悬停在它上面或选择它时,它会变回默认的灰色外观。仅当选择其他选项卡时,它才会正确显示。我怎样才能让它一直保持红色。谢谢!
如何为 tabitem 标题和该 tabitem 的内容设置不同的字体? 最佳答案 就像 WPF 中的任何东西一样,有很多方法。在不知道你到底想做什么的情况下,这里有一个“例如”(我不建议使用这种字体
之前选择的tabitem和选择的tabitem的动画开始工作正常,然后就不能正常工作了。 在我看来,代码应该可以正常工作。我不知道为什么现在动画不正确。 主窗口.xaml:
我有一个 Silverlight 应用程序,它有一个带有多个 TabItem 的 TabControl。当用户选择选项卡项时,我想将焦点设置到该选项卡项中的特定控件。我该怎么做? 我尝试为 TabCo
我在 TabControl 上有一个相当奇怪的行为,它的 TabItems 都折叠了:第一个 TabItem 的内容仍然可见(但标题不是)。 TabControl 及其 TabItems 设置如下:
下面是我的代码,当按下按钮时,我将根据我的情况使用 item1.setcontrol 更改 item1 列表。复合不刷新,但是当我单击 Item2 选项卡并返回 Item1 选项卡时...列表更新取决
这个问题有没有什么好的解决办法。 我已经设置了 tabitem.visibility=hidden 但这个 tabitem 中的内容仍然可见。我想隐藏tabitems的所有内容。 我确实搜索了一个解决
例如,使用 WPF 在选项卡控件的最左上角定位三个 tabitem 和在最右上角定位一个 tabitem 的正确方法是什么? 我尝试通过更改边距将第四个 tabitem 向右移动,但这并没有产生好的结
我正在尝试设置 TabControl 的样式,并且已经完成了 75% 的设置,但是我在设置实际 TabItems 的样式时遇到了困难: 我想要实现的是删除默认的 ContentPresenter 以便
我有一个 TabControl,其中有许多 TabItems 绑定(bind)到一个 ViewModel,该 ViewModel 具有每个 TabItem 的可见性的属性。 所有 Tab
我有一个 NSTabViewController,它有两个 NSTabViewItem。我想禁用第二个选项卡。 class MainTabViewController: NSTabViewContro
在这里,我有一个使用 Prism 6 Toolkit 的功能性 CloseTabAction,如下所示: class CloseTabAction : TriggerAction { prot
我有一个动态创建选项卡的 WPF 应用程序,现在我希望每个选项卡项都必须包含扩展器和用户控件,并且在运行时为扩展器和用户控件设置数据上下文 strong>,所以如果我在Style中定义tabitem的
我有一个问题。在我的 WPF 应用程序中,如果我用鼠标中键按下一个 tabItem,这个 tabItem 应该关闭。就像在 FireFox 中一样。但是我尝试使用 MVVM 来执行此操作,并且我需要使
您好,我正在使用 TabControl 制作 session 系统,私有(private)聊天将在新的 TabItem 中。我正在使用此代码来介绍一个新成员私下聊天: TabItem ti = new
所以我得到了一个带有一些 Tabitems 和一些按钮的 TabControl。 当我按下这些按钮时,会改变 TabStrip 的位置。 一切正常。我现在要做的是, 当我按下按钮 Right90 将标
是否可以在 TabControl 的 TabItem 中打开另一个 Window? 我想这样做的原因是,如果我的 TabControl 中有 5 个 TabItem,我正在编写所有这些 TabItem
我有一个TabControl 控件 在后面的代码中,我向 TabControl 动态添加了一些选项卡,如下所示: foreach (Village vill in Villages) {
如何向 wpf tabitem 添加一个上下文菜单,该菜单仅在我单击 tabitem 标题而不是内容时出现? 我还需要在 .cs 中动态创建 tabitems,因此在 .xaml 中静态执行此操作将不
我是一名优秀的程序员,十分优秀!