WPF – TabItem Background color changes when tabitem selected or hover over

Here is example of TabItem ControlTemplate Copy it to your resources and set wherever you need Red color as Background. SAMPLE <Window x:Class=”TestCustomTab.Window1″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”Window1″ Height=”300″ Width=”300″> <Window.Resources> <SolidColorBrush x:Key=”RedBrush” Color=”Red”/> <SolidColorBrush x:Key=”SolidBorderBrush” Color=”#888″ /> <SolidColorBrush x:Key=”GreenBrush” Color=”Green” /> <SolidColorBrush x:Key=”DisabledBackgroundBrush” Color=”#EEE” /> <SolidColorBrush x:Key=”DisabledBorderBrush” Color=”#AAA” /> <SolidColorBrush x:Key=”DisabledForegroundBrush” Color=”#888″ /> <Style TargetType=”{x:Type TabItem}”> … Read more

What is the proper way to handle multiple datagrids in a tab control so that cells leave edit mode when the tabs are changed?

I implemented a behavior for the DataGrid based on code I found in this thread. Usage:<DataGrid local:DataGridCommitEditBehavior.CommitOnLostFocus=”True” /> Code: using System.Collections.Generic; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media; /// <summary> /// Provides an ugly hack to prevent a bug in the data grid. /// https://connect.microsoft.com/VisualStudio/feedback/details/532494/wpf-datagrid-and-tabcontrol-deferrefresh-exception /// </summary> public class DataGridCommitEditBehavior … Read more

TabControl and borders visual glitch

Here’s my attempted hack. I used a NativeWindow to draw over the TabControl to fill in those “white” spaces. I won’t claim it’s perfect: public class TabPadding : NativeWindow { private const int WM_PAINT = 0xF; private TabControl tabControl; public TabPadding(TabControl tc) { tabControl = tc; tabControl.Selected += new TabControlEventHandler(tabControl_Selected); AssignHandle(tc.Handle); } void tabControl_Selected(object sender, … Read more

Close button in tabControl

Without deriving a class, here is a neat snippet: http://www.dotnetthoughts.net/implementing-close-button-in-tab-pages/ Set the DrawMode property of the Tab Control to OwnerDrawFixed. This property decides whether system or developer can paint the captions. Add the code in the DrawItem event of the Tab Control – This event will be invoked for painting each Tab Page. //This code … Read more

TabControl with Add New Tab Button (+)

An almost complete solution using IEditableCollectionView: ObservableCollection<ItemVM> _items; public ObservableCollection<ItemVM> Items { get { if (_items == null) { _items = new ObservableCollection<ItemVM>(); var itemsView = (IEditableCollectionView)CollectionViewSource.GetDefaultView(_items); itemsView.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtEnd; } return _items; } } private DelegateCommand<object> _newCommand; public DelegateCommand<object> NewCommand { get { if (_newCommand == null) { _newCommand = new DelegateCommand<object>(New_Execute); } return … Read more

How do I disable visual styles for just one control, and not its children?

Add a new class to your project and paste the code shown below. Build. Drop the new control from the top of the toolbox onto your form. Visual styles of the child controls are preserved. using System; using System.Windows.Forms; using System.Runtime.InteropServices; public class FixedTabControl : TabControl { [DllImportAttribute(“uxtheme.dll”)] private static extern int SetWindowTheme(IntPtr hWnd, string … Read more

Close button for TabPages of Right To Left TabControl c#

You can create a function to translate coordinates of a rectangle to RTL coordinates in a container: public static Rectangle GetRTLCoordinates(Rectangle container, Rectangle drawRectangle) { return new Rectangle( container.Width – drawRectangle.Width – drawRectangle.X, drawRectangle.Y, drawRectangle.Width, drawRectangle.Height); } And when painting in RTL mode, calculate coordinates this way: tabRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, tabRect); Also you should draw … Read more

In C# WPF, why is my TabControl’s SelectionChanged event firing too often?

The TabControl.SelectionChanged is the same event as a ComboBox.SelectionChanged It originates from Selector.SelectionChanged. So, if you do not mark your event as handled in your event handler, it will bubble up the tree, and eventually arrive at your TabControl, which is causing this “firing too often” issue. Mark your event as handled in your SelectionChanged … Read more

How to create trapezoid tabs in WPF tab control

I tried to find some control templates or solutions for this problem on internet, but I didn’t find any “acceptable” solution for me. So I wrote it in my way and here is an example of my first (and last=)) attempt to do it: <Window x:Class=”TabControlTemplate.Window1″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:src=”https://stackoverflow.com/questions/561931/clr-namespace:TabControlTemplate” Title=”Window1″ Width=”600″ Height=”400″> <Window.Background> <LinearGradientBrush StartPoint=”0,0″ … Read more