- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 WPF TabControl,其最后一项始终是添加新选项卡的按钮,类似于 Firefox:
TabControl 的 ItemSource 绑定(bind)到一个 ObservableCollection,通过这个“+”按钮向集合中添加一个项目非常有效。我遇到的唯一问题是,在单击“+”选项卡后,我无法将新创建的(或任何其他现有选项卡)设置为焦点,因此当添加选项卡时,用户界面看起来像这样:
为了解释一下我是如何实现这种“特殊”选项卡行为的,TabControl 是模板化的,它的 NewButtonHeaderTemplate 有一个控件(在我的例子中是图像),它在 View 模型中调用 AddListener 命令(仅显示相关代码):
<Window x:Class="AIS2.PortListener.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ais="http://www.leica-geosystems.com/xaml"
xmlns:l="clr-namespace:AIS2.PortListener"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
DataContext="{Binding Source={StaticResource Locator}>
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="newTabButtonHeaderTemplate">
<Grid>
<Image Source="..\Images\add.png" Height="16" Width="16">
</Image>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand
Command="{Binding Source={StaticResource Locator},
Path=PortListenerVM.AddListenerCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</DataTemplate>
<DataTemplate x:Key="newTabButtonContentTemplate"/>
<DataTemplate x:Key="itemHeaderTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<DataTemplate x:Key="itemContentTemplate">
<l:ListenerControl></l:ListenerControl>
</DataTemplate>
<l:ItemHeaderTemplateSelector x:Key="headerTemplateSelector"
NewButtonHeaderTemplate="{StaticResource newTabButtonHeaderTemplate}"
ItemHeaderTemplate="{StaticResource itemHeaderTemplate}"/>
<l:ItemContentTemplateSelector x:Key="contentTemplateSelector"
NewButtonContentTemplate="{StaticResource newTabButtonContentTemplate}"
ItemContentTemplate="{StaticResource itemContentTemplate}"/>
</ResourceDictionary>
</Window.Resources>
<TabControl Name="MainTab" Grid.Row="2" ItemsSource="{Binding Listeners}"
ItemTemplateSelector="{StaticResource headerTemplateSelector}"
ContentTemplateSelector="{StaticResource contentTemplateSelector}"
SelectedItem="{Binding SelectedListener}">
</TabControl>
private ObservableCollection<Listener> _Listeners;
public ObservableCollection<Listener> Listeners
{
get { return _Listeners; }
}
private object _SelectedListener;
public object SelectedListener
{
get { return _SelectedListener; }
set
{
_SelectedListener = value;
OnPropertyChanged("SelectedListener");
}
}
public PortListenerViewModel()
{
// Place the "+" tab at the end of the tab control
var itemsView = (IEditableCollectionView)CollectionViewSource.GetDefaultView(_Listeners);
itemsView.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtEnd;
}
private RelayCommand _AddListenerCommand;
public RelayCommand AddListenerCommand
{
get
{
if (_AddListenerCommand == null)
_AddListenerCommand = new RelayCommand(param => this.AddListener());
return _AddListenerCommand;
}
}
public void AddListener()
{
var newListener = new TCPListener(0, "New listener");
this.Listeners.Add(newListener);
// The following two lines update the property, but the focus does not change
//this.SelectedListener = newListener;
//this.SelectedListener = this.Listeners[0];
}
set
中设置断点我可以看到以下情况发生:
this.Listeners.Add(newListener);
this.SelectedListener = newListener;
set
使用正确的 Listener 对象调用 set
使用 NewItemPlaceholder 对象(根据调试器为 MS.Internal.NamedObject 类型)调用 最佳答案
我认为当您单击新选项卡时会触发两个事件:MouseLeftButtonDown
和 TabControl.SelectionChanged
我认为他们都在排队,然后一次处理一个。
因此,您的项目被添加,设置为选中,然后在重新绘制之前发生 SelectionChanged 事件以将选择更改为 [+]
标签。
也许尝试使用 Dispatcher
设置SelectedItem
所以它发生在 TabControl
之后改变它的选择。或者,如果用户尝试切换到 NewTab,它会取消 SelectionChanged
事件,因此所选选项卡实际上不会更改(当然,SelectedTab
将是您的 NewItem,因为会发生 MouseDown 事件)
当我过去做这样的事情时,我实际上覆盖了 TabControl Template将 AddTab 按钮创建为 Button
,而不是 TabItem
.我想建议这样做而不是使用 NewItemPlaceholder
首先,但我从未尝试过使用 NewItemPlaceholder
所以真的不知道它是否比覆盖 Template
更好或更差.
关于wpf - 添加选项卡时,TabControl 的 SelectedItem 被 NewItemPlaceholder 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8944314/
我正在绑定(bind) TabViewModel 的集合项目到 TabControl .每一个都有一个标题 string属性,以及我自己的自定义类型的内容属性 BaseTabContentViewMo
嘿,我一直在尝试绘制自己的 TabControl 以摆脱 3D 阴影,但我没有取得太大的成功。 DrawItem 事件目前未触发。必须自己拍吗?我该怎么做? 代码: namespace NCPad {
我遇到了 tabcontrol 的问题。当我将 DrawMode 更改为 ownderdrawfixed 时,tabcontrol 的 borderstyle 从“fixedsingle”更改为“3d
我的非数据绑定(bind) TabControl 看起来不错: alt text http://tanguay.info/web/external/tabControlPlain.png
是否可以使 tabcontrol 边框透明,或设置 tabcontrol 的颜色? Winforms 最佳答案 如果有人遇到同样的问题,这是对我有用的答案 Border TabControl 正如它所
我正在为我的应用程序使用 MVVM 模式。 MainWindow 包含一个 TabControl,其中 DataContext 映射到 ViewModel:
我使用的是未指定高度和宽度的 TabControl。实际上,TabControl 的高度取当前选项卡的高度。 我不希望 TabControl 自动调整它的大小。我希望它采用最大标签的大小(高度和宽度)
我正在使用 WinForms 并且在某个时候我无法在我的 Win 窗体中找到 TabControl 用户的标题的高度, 下面我附上我想要实现的圈内图像,我已经搜索了很多,但我无法找到解决方案 最佳答案
我在选项卡控件的顶部有 4 个选项卡。我希望每个选项卡使用 TabControl 宽度的 25%。 使用 XAML 做到这一点的正确方法是什么? 这是我尝试过的:
我想设置一个 TabControl 使其标题位于左侧而不是顶部,我该怎么做? 最佳答案 关于wpf - TabControl.Orientation?,我们在Stack Overflow上找到一个
有谁知道为什么 TabControl 的 padding 属性不能用经典主题呈现但适用于 luna 主题? XAML 非常基础。我已经将左填充设为 50,这样问题在屏幕截图中就很明显了。
我有一个 WPF TabControl,包含两个不同宽度和高度的 TabItem。奇怪的是,当我在项目之间切换时,TabControl 的大小被调整为紧密适合所选选项卡项目。例如,如果我单击较小的 T
我必须开发一个自定义选项卡控件并决定使用 WPF/XAML 创建它,因为我无论如何都打算学习它。完成后应该是这样的: 到目前为止,我取得了很好的进展,但还有两个问题: 只有第一个/最后一个标签项应该有
嗨,我有两个问题。 如何以编程方式将 WPF 选项卡控件中的选定选项卡从一个选项卡更改为另一个选项卡。 如何获得对要在其中设置选定选项卡的“其他选项卡”的引用? 最佳答案 使用 SelectedInd
我对 TabControl 有疑问, 一个 TextBox和验证ToolTip . 想象一下有一个带有两个 TabItem 的 TabControl。第一项有一个简单的 TextBox .这个Text
我有一个选项卡控件,用于在应用程序中显示多个图像文件。我想在仅显示一个选项卡页时删除选项卡页标题,以便我可以使用该屏幕空间来显示图像。 (这类似于在 Firefox 中取消选择“始终显示选项卡栏”。)
我的 tabcontrol 中有 7 个 tabitem,而 tabcontrol 的宽度只有 500。因此 tabitem 显示在 3 行中。我希望它像溢出选项卡控件一样显示在选项卡控件末尾的下拉列
我有一个要自定义的选项卡控件。更具体地说,我想更改标签页标题的颜色,以及标签页周围白线的颜色(检查第一张图片)。 我想过使用自定义渲染器来执行此操作(例如,类似于为菜单条重新着色),但我不确定如何执行
我正在用单个窗口中的 TabControl 编写 WPF 桌面应用程序。我在 XAML View 中对一些属性进行了数据绑定(bind),只要在 .cs 文件的构造函数中更改了值,它就可以正常工作。稍
我有一个应用程序在 Vista 中运行时将每个控件的字体更改为 SegoeUI。它工作正常,除了标签页的标题(从一个标签切换到另一个标签时要单击的按钮)。 标签页标题不会垂直增长以适应更大的字体大小,
我是一名优秀的程序员,十分优秀!