tabcontrol 相关问题

选项卡控件是图形用户界面中的控件,其包含在屏幕上共享相同空间的多个选项卡项。

一个WPF MainWindow有四个内容相同的TabItem(一个View,ViewModel),如何告诉View它属于哪个TabItem?

我探索/学习 WPF/MVVM 的下一步将我带到了带有 TabControl 和四个 TabItem 的 MainWindow。每个选项卡都有相同的内容,在一个视图中定义一组四个复选框,结果...

回答 0 投票 0

任何人都可以帮助我我无法在选项卡控件 C# 中初始化我的选项卡页面的名称属性这是我的代码

使用(连接=新的SqlConnection(CS)) { SqlDataAdapter 适配器 = new SqlDataAdapter("SELECT category_name FROM book_category", Connection); ...

回答 0 投票 0

如何取消 WPF TabControl 中的选项卡更改

我在 SO 上发现了多个关于这个问题的问题,但是我仍然无法找到一个可靠的解决方案。这是我在阅读答案后想到的。 Xaml: 我在 SO 上发现了关于这个问题的多个问题,但是我仍然无法找到一个可靠的解决方案。这是我在阅读答案后想到的。 Xaml: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="300" Width="300" x:Name="this"> <TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Tabs, ElementName=this}" x:Name="TabControl"/> </Window> 背后的代码: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var tabs = new ObservableCollection<string> {"Tab1", "Tab2", "Tab3"}; Tabs = CollectionViewSource.GetDefaultView(tabs); Tabs.CurrentChanging += OnCurrentChanging; Tabs.CurrentChanged += OnCurrentChanged; Tabs.MoveCurrentToFirst(); CurrentTab = tabs.First(); } private void OnCurrentChanging(object sender, CurrentChangingEventArgs e) { //only show message box when tab is changed by user input if (!_cancelTabChange) { if (MessageBox.Show("Change tab?", "Message", MessageBoxButton.YesNo) == MessageBoxResult.No) { _cancelTabChange = true; return; } } _cancelTabChange = false; } private void OnCurrentChanged(object sender, EventArgs e) { if (!_cancelTabChange) { //Update current tab property, if user did not cancel transition CurrentTab = (string)Tabs.CurrentItem; } else { //navigate back to current tab otherwise Dispatcher.BeginInvoke(new Action(() => Tabs.MoveCurrentTo(CurrentTab))); } } public string CurrentTab { get; set; } public static readonly DependencyProperty TabsProperty = DependencyProperty.Register("Tabs", typeof(ICollectionView), typeof(MainWindow), new FrameworkPropertyMetadata(default(ICollectionView))); public ICollectionView Tabs { get { return (ICollectionView)GetValue(TabsProperty); } set { SetValue(TabsProperty, value); } } private bool _cancelTabChange; } 基本上我想显示一条确认消息,当用户导航到不同的选项卡时,如果他单击“否” - 中止转换。但是这段代码不起作用。如果多次单击“Tab2”,每次都在消息框中选择“否”,它会在某个时候停止工作:事件停止触发。如果您单击“Tab3”,事件将再次触发,但如果您选择“是”,它将打开第二个选项卡而不是第三个选项卡。我无法弄清楚 wtf 是怎么回事。 :) 有人看到我的解决方案中有错误吗?或者有没有更简单的方法来显示确认消息,当用户切换选项卡?我也愿意使用任何具有适当 SelectionChanging 事件的开源选项卡控件。我找不到任何东西。 我正在使用.Net 4.0. 编辑: 如果我把消息框注释掉: private void OnCurrentChanging(object sender, CurrentChangingEventArgs e) { //only show message box when tab is changed by user input if (!_cancelTabChange) { //if (MessageBox.Show("Change tab?", "Message", MessageBoxButton.YesNo) == MessageBoxResult.No) //{ Debug.WriteLine("Canceled"); _cancelTabChange = true; return; //} } _cancelTabChange = false; } 一切正常。奇怪。 这个解决方案来自web.archive 似乎与一起工作得很好 <TabControl ... yournamespace:SelectorAttachedProperties.IsSynchronizedWithCurrentItemFixEnabled="True" .../> private void OnCurrentChanging(object sender, CurrentChangingEventArgs e) { if (MessageBox.Show("Change tab?", "Message", MessageBoxButton.YesNo) == MessageBoxResult.No) { e.Cancel = true; } } public static class SelectorAttachedProperties { private static Type _ownerType = typeof(SelectorAttachedProperties); #region IsSynchronizedWithCurrentItemFixEnabled public static readonly DependencyProperty IsSynchronizedWithCurrentItemFixEnabledProperty = DependencyProperty.RegisterAttached("IsSynchronizedWithCurrentItemFixEnabled", typeof(bool), _ownerType, new PropertyMetadata(false, OnIsSynchronizedWithCurrentItemFixEnabledChanged)); public static bool GetIsSynchronizedWithCurrentItemFixEnabled(DependencyObject obj) { return (bool)obj.GetValue(IsSynchronizedWithCurrentItemFixEnabledProperty); } public static void SetIsSynchronizedWithCurrentItemFixEnabled(DependencyObject obj, bool value) { obj.SetValue(IsSynchronizedWithCurrentItemFixEnabledProperty, value); } private static void OnIsSynchronizedWithCurrentItemFixEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Selector selector = d as Selector; if (selector == null || !(e.OldValue is bool && e.NewValue is bool) || e.OldValue == e.NewValue) return; bool enforceCurrentItemSync = (bool)e.NewValue; ICollectionView collectionView = null; EventHandler itemsSourceChangedHandler = null; itemsSourceChangedHandler = delegate { collectionView = selector.ItemsSource as ICollectionView; if (collectionView == null) collectionView = CollectionViewSource.GetDefaultView(selector); }; SelectionChangedEventHandler selectionChangedHanlder = null; selectionChangedHanlder = delegate { if (collectionView == null) return; if (selector.IsSynchronizedWithCurrentItem == true && selector.SelectedItem != collectionView.CurrentItem) { selector.IsSynchronizedWithCurrentItem = false; selector.SelectedItem = collectionView.CurrentItem; selector.IsSynchronizedWithCurrentItem = true; } }; if (enforceCurrentItemSync) { TypeDescriptor.GetProperties(selector)["ItemsSource"].AddValueChanged(selector, itemsSourceChangedHandler); selector.SelectionChanged += selectionChangedHanlder; } else { TypeDescriptor.GetProperties(selector)["ItemsSource"].RemoveValueChanged(selector, itemsSourceChangedHandler); selector.SelectionChanged -= selectionChangedHanlder; } } #endregion IsSynchronizedWithCurrentItemFixEnabled } 出于某种原因,添加 TabControl.Focus() 可以解决问题: private void OnCurrentChanged(object sender, EventArgs e) { if (!_cancelTabChange) { //Update current tab property, if user did not cancel transition CurrentTab = (string)Tabs.CurrentItem; } else { //navigate back to current tab otherwise Dispatcher.BeginInvoke(new Action(() => { Tabs.MoveCurrentTo(CurrentTab); TabControl.Focus(); })); } } 我仍然不知道地球上到底发生了什么。所以我很乐意接受这个答案,这对这个问题有所启发。 在 tabControl_SelectionChanged 事件处理程序中: if (e.OriginalSource == tabControl) //if this event fired from your tabControl { e.Handled = true; if (!forbiddenPage.IsSelected) //User leaving the tab { if (forbiddenTest()) { forbiddenPage.IsSelected = true; MessageBox.Show("you must not leave this page"); } } 注意设置forbiddenPage.IsSelected = true会导致循环,你重新进入 这个事件处理程序。然而,这次我们退出,因为所选页面是禁止页面。 private void MainTabControl_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (ReasonBecauseLeaveTabItemIsForbidden) { if (MainTabControl.SelectedIndex == IndexOfTabItem) { MessageBox.Show(SomeMessageWhyLeaveTabItemIsForbidden); } MainTabControl.SelectedIndex = IndexOfTabItem; } } IndexOfTabItem - 因离开而禁用的 TabItem 的索引。 必须服从的人要求应用程序询问用户是否要离开页面,因此这里是稍微更改的代码: private Object _selectedTab; public Object SelectedTab { get { return _selectedTab; } set { if ( !(_selectedTab is ADR_Scanner.ViewModel.ConfigurationViewModel) || !_configurationViewModel.HasChanged || (System.Windows.Forms.MessageBox.Show("Are you sure you want to leave this page without saving the configuration changes", ADR_Scanner.App.Current.MainWindow.Title, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Yes) ) { _selectedTab = value; } OnPropertyChanged("SelectedTab"); } } 我认为这个小改动几乎可以满足您的需求。 有一个更简单的解决方案。向 XAML 中的选定项添加绑定: <TabControl SelectedItem="{Binding SelectedTab}" ... 然后在视图模型中: private Object _selectedTab; public Object SelectedTab { get { return _selectedTab; } set { if (_selectedTab is ADR_Scanner.ViewModel.ConfigurationViewModel && _configurationViewModel.HasChanged) { System.Windows.Forms.MessageBox.Show("Please save the configuration changes", ADR_Scanner.App.ResourceAssembly.GetName().Name, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } else { _selectedTab = value; } OnPropertyChanged("SelectedTab"); } } 显然,您将 ADR_Scanner.ViewModel.ConfigurationViewModel 替换为您自己的视图模型类。最后确保在构造函数中初始化 _selectedTab,否则 TabControl 将没有初始选择。

回答 6 投票 0

标准WPF选项卡控件中是否有Selected Tab Changed事件

在 WPF 中,是否有一个事件可用于确定 TabControl 的选定选项卡何时更改? 我试过使用 TabControl.SelectionChanged 但它在孩子的

回答 10 投票 0

如何在使用 C# 上课时调整所有内容的大小? [关闭]

每次创建新标签时,内容都会出现,但当整个页面调整大小时,内容不会调整大小。它在没有课程的情况下工作正常,但每当我尝试将它与课程集成时......

回答 0 投票 0

ListView 和 TextBox OnPropertyChanged 工作正常但 Tab Control 不工作

过去几周,当我在列表视图文本框中更改值时,我努力解决在选项卡控件中进行更改的问题,反之亦然。 列表视图和文本框一切正常,但我不是...

回答 0 投票 0

VB.NET 中的 TabControl 对齐问题

我在 VB.NET 中使用左对齐的 TabControl 时出现奇怪的行为。截屏: 我想要的是让选项卡与向左旋转 90 度时的字面意思相同。 母鹿...

回答 2 投票 0

如何在c#回车代码中访问TabControl.ContentTemplate中的内容?

我写的是一个矢量编辑器。在我开始使用TabControl之前,一切都很正常。然而,有必要同时上传几个打开的文件。每个打开的文件都显示在TabItem上。...

回答 1 投票 0

WPF Tab控件。鼠标点击事件(空区域)标签栏

在Firefox中,双击标签栏的空区域可以创建一个新的标签。我想在WPF TabControl上实现这个功能。但如何处理鼠标点击事件?我试着处理了...

回答 3 投票 1

如何从不同的ObservableCollection集合绑定到ContentTemplate中的每个canvas?

关于这个问题,我看了几个答案,还是不明白。如何从不同的ObservableCollection集合绑定到ContentTemplate中的每个画布?

回答 1 投票 0

当SelectedObject发生变化时,WPF Tab控件会保留所选的标签。

我正在使用WPF TabControl,并得到了一个简单的问题(我希望)。我得到了一个项目列表,每个项目的详细信息都显示在一个有3个Tab的TabControl中。

回答 1 投票 0

禁止在选中的TabItem上改变边框和背景。

在我的wpf文档中,我有这样的内容。 <Setter Property="Foreground" Value="#303030"></Setter> ...</root>

回答 1 投票 0

如何让Tabcontrol在TabItem改变前弹出一条信息?

我在一个情况下,我使用TabControl的Selectionchanged事件来改变TabItem。我的做法是:tabcntrl.SelectionChanged += new SelectionChangedEventHandler( obj...)。

回答 2 投票 1

复制时所有选项卡上可见的按钮

当我在 Access 中并使用选项卡控件时,如果我在一个选项卡上复制一个按钮并将其粘贴到同一选项卡,则该按钮在所有选项卡上都可见。事实上,我认为所有控件都可以(我知道它发生了......

回答 2 投票 0

WPF选项卡控件仅对第一个选项卡使用不同的ItemTemplate

我正在使用带有TabControl的WPF。我定义了一个ItemTemplate,它为标题设置一个TextBlock,并为关闭选项卡设置一个按钮。我希望第一个标签不显示关闭按钮。我是...

回答 1 投票 0

如何在Tabcontrol.Pages来自GUI设计器之后在Tabcontrol上动态添加复选框元素?

我可以在程序启动后添加tabcontrol元素吗?我使用来自GUI-Designer的Tabcontrol并想操纵TabPage。我希望在Formular _load上有新的动态复选框。我看不到这些...

回答 1 投票 0

如何向TabControl添加新标签并能够更新其中的控件?

我需要能够以编程方式在TabControl上创建新标签页,向其添加控件,并能够通过其他功能更新每个标签页中的控件。我已经具有添加标签的功能...

回答 3 投票 1

如何以编程方式向TabControl中添加的TabView停靠在TabControl中?

我正在使用Visual Basic 9(VS2008),当用户单击“添加选项卡”按钮时,我想创建新的选项卡。该选项卡中必须停靠一个ListView控件。如何以编程方式添加选项卡...

回答 2 投票 1

如何Button.PerformClick非活动的TabPage?

我正在一个具有TabControl和5个TabPage控件的项目中,每个TabPage至少有10个按钮启动不同的窗体。在表单的Load事件中,我遍历所有TabPage和...

回答 2 投票 0

TabControl命令绑定

我正在使用此代码,以下代码取自Stackoverflow。我想在SelectionChanged事件中将字符串从一个视图模型转移到另一个视图模型。但是当我单击Tab2时,会收到Tab2消息...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.