Vertical Text on Right-Aligned TabControl - winforms

Trying to have the text on each tab of the tabcontrol to be displayed vertically. Being that I've never forayed into controls and what not, i'm sorta stuck. Found some code to get the text to display as if it were rotated to the left. I would like it to display as if it were rotated right (vertically) on the tab. The skeleton code is below:
Protected Sub OnDrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles TabControl1.DrawItem
'MyBase.OnDrawItem(e)'
Dim tc As TabControl = DirectCast(sender, TabControl)
Dim g As Graphics = e.Graphics
Dim rectf As RectangleF
Dim isVertical As Boolean = (tc.Alignment > TabAlignment.Bottom)
Dim off As Integer = 1 : If (e.State And sel) = sel Then off = -1
Dim textFormat As New StringFormat(StringFormatFlags.NoClip _
Or StringFormatFlags.NoWrap)
With textFormat
.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show
.Alignment = StringAlignment.Center
.LineAlignment = StringAlignment.Center
End With
With e.Bounds
If isVertical Then
' tabs are aligned left or right'
If tc.Alignment = TabAlignment.Left Then
Dim m As New System.Drawing.Drawing2D.Matrix
m.Translate(0, .Height - tc.TabPages(0).Top)
m.RotateAt(270, New PointF(.X, .Y))
g.Transform = m
rectf = New RectangleF(.Left - tc.TabPages(0).Top, .Top + off, _
.Height, .Width)
ElseIf tc.Alignment = TabAlignment.Right Then
'Dim m As New System.Drawing.Drawing2D.Matrix'
'm.Translate(0, .Height - tc.TabPages(0).Top)'
'm.RotateAt(270, New PointF(.X, .Y))'
'g.Transform = m'
'rectf = New RectangleF(.Left - tc.TabPages(0).Top, .Top + off, _'
' .Height, .Width)'
' Here is where the tab should go to rotate the text about 180 degrees'
End If
Else
' tabs are aligned top or bottom'
rectf = New RectangleF(.X, .Y + off, .Width, .Height)
End If
End With
Dim col As Color
Select Case (e.State And notsf)
Case DrawItemState.Disabled
col = SystemColors.GrayText
Case DrawItemState.HotLight
col = SystemColors.HotTrack
Case Else
col = SystemColors.MenuText
End Select
g.DrawString(tc.TabPages(e.Index).Text, _
tc.Font, _
New SolidBrush(col), _
rectf, _
textFormat)
If isVertical Then g.ResetTransform()
If (e.State And selfoc) = selfoc Then
ControlPaint.DrawFocusRectangle(g, _
[Rectangle].Inflate(e.Bounds, -1, -1))
End If
textFormat.Dispose()
End Sub

I assume you are using XAML and this achievable through styles
Add these styles:
<!-- Simple Splitter Style -->
<Style x:Key="SimpleSplitterStyle" TargetType="controls:GridSplitter">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:GridSplitter">
<Grid x:Name="Root" IsHitTestVisible="{TemplateBinding IsEnabled}">
<Rectangle Fill="{TemplateBinding Background}" StrokeThickness="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Tab Item Header (Right) -->
<Style x:Key="RightTabItemHeader" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<ContentPresenter Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" ContentTemplate="{TemplateBinding ContentTemplate}">
<layout:LayoutTransformer >
<layout:LayoutTransformer.LayoutTransform>
<RotateTransform Angle="90"/>
</layout:LayoutTransformer.LayoutTransform>
<ContentPresenter Margin="5,0,5,0" />
</layout:LayoutTransformer>
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Tab Item Header (Left) -->
<Style x:Key="LeftTabItemHeader" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<ContentPresenter Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" ContentTemplate="{TemplateBinding ContentTemplate}">
<layout:LayoutTransformer >
<layout:LayoutTransformer.LayoutTransform>
<RotateTransform Angle="-90"/>
</layout:LayoutTransformer.LayoutTransform>
<ContentPresenter Margin="5,0,5,0" />
</layout:LayoutTransformer>
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Tab Item Style -->
<Style x:Key="TabItemStyle" TargetType="controls:TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:TabItem">
<Grid x:Name="Root">
<Grid x:Name="TemplateTopSelected" Visibility="Collapsed" Canvas.ZIndex="1">
<Border Margin="-2,-2,-2,0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" CornerRadius="3,3,0,0">
<Border BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="1,1,0,0">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Rectangle Fill="#FFFFFFFF" Margin="0,0,0,-2"/>
<ContentControl x:Name="HeaderTopSelected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
</Grid>
</Border>
</Border>
<Border x:Name="FocusVisualTop" Margin="-2,-2,-2,0" IsHitTestVisible="false" Visibility="Collapsed" BorderBrush="#FF6DBDD1" BorderThickness="1,1,1,0" CornerRadius="3,3,0,0"/>
<Border x:Name="DisabledVisualTopSelected" Margin="-2,-2,-2,0" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="3,3,0,0"/>
</Grid>
<Grid x:Name="TemplateTopUnselected" Visibility="Collapsed">
<Border x:Name="BorderTop" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="3,3,0,0">
<Border x:Name="GradientTop" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="1,1,0,0">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<ContentControl x:Name="HeaderTopUnselected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
</Grid>
</Border>
</Border>
<Border x:Name="DisabledVisualTopUnSelected" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="3,3,0,0"/>
</Grid>
<Grid x:Name="TemplateBottomSelected" Visibility="Collapsed" Canvas.ZIndex="1">
<Border Margin="-2,0,-2,-2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,0,1,1" CornerRadius="0,0,3,3">
<Border BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="0,0,1,1">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Rectangle Fill="#FFFFFFFF" Margin="0,-2,0,0"/>
<ContentControl x:Name="HeaderBottomSelected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
</Grid>
</Border>
</Border>
<Border x:Name="FocusVisualBottom" Margin="-2,0,-2,-2" IsHitTestVisible="false" Visibility="Collapsed" BorderBrush="#FF6DBDD1" BorderThickness="1,0,1,1" CornerRadius="0,0,3,3"/>
<Border x:Name="DisabledVisualBottomSelected" Margin="-2,0,-2,-2" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="0,0,3,3"/>
</Grid>
<Grid x:Name="TemplateBottomUnselected" Visibility="Collapsed">
<Border x:Name="BorderBottom" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="0,0,3,3">
<Border x:Name="GradientBottom" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="0,0,1,1">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<ContentControl x:Name="HeaderBottomUnselected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
</Grid>
</Border>
</Border>
<Border x:Name="DisabledVisualBottomUnSelected" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="0,0,3,3"/>
</Grid>
<Grid x:Name="TemplateLeftSelected" Visibility="Collapsed" Canvas.ZIndex="1">
<Border Margin="-2,-2,0,-2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,0,1" CornerRadius="3,0,0,3">
<Border BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="1,0,0,1">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Rectangle Fill="#FFFFFFFF" Margin="0,0,-2,0"/>
<ContentControl x:Name="HeaderLeftSelected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Style="{StaticResource LeftTabItemHeader}"/>
</Grid>
</Border>
</Border>
<Border x:Name="FocusVisualLeft" Margin="-2,-2,0,-2" IsHitTestVisible="false" Visibility="Collapsed" BorderBrush="#FF6DBDD1" BorderThickness="1,1,0,1" CornerRadius="3,0,0,3"/>
<Border x:Name="DisabledVisualLeftSelected" Margin="-2,-2,0,-2" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="3,0,0,3"/>
</Grid>
<Grid x:Name="TemplateLeftUnselected" Visibility="Collapsed">
<Border x:Name="BorderLeft" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3,0,0,3">
<Border x:Name="GradientLeft" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="1,0,0,1">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<ContentControl x:Name="HeaderLeftUnselected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Style="{StaticResource LeftTabItemHeader}"/>
</Grid>
</Border>
</Border>
<Border x:Name="DisabledVisualLeftUnSelected" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="3,0,0,3"/>
</Grid>
<Grid x:Name="TemplateRightSelected" Visibility="Collapsed" Canvas.ZIndex="1">
<Border Margin="0,-2,-2,-2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,1,1,1" CornerRadius="0,3,3,0">
<Border BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="0,1,1,0">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Rectangle Fill="#FFFFFFFF" Margin="-2,0,0,0"/>
<ContentControl x:Name="HeaderRightSelected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Style="{StaticResource RightTabItemHeader}"/>
</Grid>
</Border>
</Border>
<Border x:Name="FocusVisualRight" Margin="0,-2,-2,-2" IsHitTestVisible="false" Visibility="Collapsed" BorderBrush="#FF6DBDD1" BorderThickness="0,1,1,1" CornerRadius="0,3,3,0"/>
<Border x:Name="DisabledVisualRightSelected" Margin="0,-2,-2,-2" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="0,3,3,0"/>
</Grid>
<Grid x:Name="TemplateRightUnselected" Visibility="Collapsed">
<Border x:Name="BorderRight" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="0,3,3,0">
<Border x:Name="GradientRight" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="0,1,1,0">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<ContentControl x:Name="HeaderRightUnselected" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" IsTabStop="False" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Style="{StaticResource RightTabItemHeader}"/>
</Grid>
</Border>
</Border>
<Border x:Name="DisabledVisualRightUnSelected" IsHitTestVisible="false" Opacity="0" Background="#8CFFFFFF" CornerRadius="0,3,3,0"/>
</Grid>
<Border x:Name="FocusVisualElement" Margin="-1" IsHitTestVisible="false" Visibility="Collapsed" BorderBrush="#FF6DBDD1" BorderThickness="1" CornerRadius="3,3,0,0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And Then you can assign this to each tab item:
<controls:TabControl TabStripPlacement="Right" Margin="4">
<controls:TabItem Header="First" Style="{StaticResource TabItemStyle}">
<controls:TreeView>
<controls:TreeViewItem Header="Parent 1" />
<controls:TreeViewItem Header="Parent 2">
<controls:TreeViewItem Header="Child 1">
<controls:TreeViewItem Header="Grandchild 1" />
</controls:TreeViewItem>
</controls:TreeViewItem>
<controls:TreeViewItem Header="Parent 3">
<controls:TreeViewItem Header="Child 1">
<controls:TreeViewItem Header="Grandchild 1" />
<controls:TreeViewItem Header="Grandchild 2" />
</controls:TreeViewItem>
</controls:TreeViewItem>
</controls:TreeView>
</controls:TabItem>
<controls:TabItem Header="Second" Style="{StaticResource TabItemStyle}">
<controls:TreeView>
<controls:TreeViewItem Header="Parent 1" />
<controls:TreeViewItem Header="Parent 2">
<controls:TreeViewItem Header="Child 1">
<controls:TreeViewItem Header="Grandchild 1" />
</controls:TreeViewItem>
</controls:TreeViewItem>
<controls:TreeViewItem Header="Parent 3">
<controls:TreeViewItem Header="Child 1">
<controls:TreeViewItem Header="Grandchild 1" />
<controls:TreeViewItem Header="Grandchild 2" />
</controls:TreeViewItem>
</controls:TreeViewItem>
<controls:TreeViewItem Header="Parent 4">
<controls:TreeViewItem Header="Child 1">
<controls:TreeViewItem Header="Grandchild 1" />
<controls:TreeViewItem Header="Grandchild 2" />
<controls:TreeViewItem Header="Grandchild 3" />
<controls:TreeViewItem Header="Grandchild 4" />
<controls:TreeViewItem Header="Grandchild 5" />
<controls:TreeViewItem Header="Grandchild 6" />
<controls:TreeViewItem Header="Grandchild 7" />
<controls:TreeViewItem Header="Grandchild 8" />
</controls:TreeViewItem>
</controls:TreeViewItem>
</controls:TreeView>
</controls:TabItem>
</controls:TabControl>
Now you can Change from Top/Bottom/Right/Left and the text will go vertical on Right/Left. I left top and bottom alone in this example but this should give you plenty of ideas on how to control these tabs.

I don't use VB or WinForms, so the following won't be in code and includes a couple of guesses. It's also hard to know what's different from what you want from your description without a screenshot. Am I right in thinking you just want your tab's caption text rotated 180 degrees the other way, ie the "top" of the text against the left side of the tab or the right side of the tab, whichever one it isn't now?
In the branch If tc.Alignment = TabAlignment.Right Then ... your text is being rotated by the RotateAt call:
m.RotateAt(270, New PointF(.X, .Y))
and you'll want to change 270 to 90 to have the text rotated in the other direction.
(Are you familiar with setting up transformation matrices like this? The same principles apply in OpenGL or DirectX programming. If not, comment and I'll explain / link.)
If you just do that then chances are the text will not be visible, because it will be rotated around to off the bitmap you're drawing on. You will need to translate it so that the beginning of the text is in the right corner. This is where the guesswork I mentioned above comes in: I'm not familiar with the coordinate system of the graphics framework you're using, so I don't know which direction(s) to translate. I'm sorry I can't give you working code, but I just don't have Visual Studio and a .Net Winforms control to experiment with :) I can see you're already doing something along these lines here:
m.Translate(0, .Height - tc.TabPages(0).Top)
My guess is you'd need to change this either so the X coordinate (0) is .Width or .Width - [text height, calculate this], and you may need to also account for the text width in the Y coordinate. (Text "height" and "width" are as though the text is being drawn horizontally / normally.) I'm pretty certain .Net's drawing framework probably includes Graphics TextHeight(...) and TextWidth(...) methods and if it doesn't by those names I'm sure they're easy to find :) TextRect() perhaps?
(If I was sitting at your PC and trying to figure this out by pure guesswork I'd write this code so it rotated the text in the very middle of a large bitmap, modify it so it's in the top left of the bitmap in the right spot inside a rectangle the size of a tab, and then move the code to the tab drawing method. Saves all the trouble of having a tiny little tab and running it and going "Where's my text?" when it's invisible because it's being drawn at the wrong coordinates.)

Related

Add Text beyond all Stack column charts in wpf

I want to draw a report with Stack Column Chart in Wpf,
I can add text inside each chart to represent the value,
here is my code to do so :
<Grid Background="{TemplateBinding Background}">
<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush>
<GradientStop Color="#77ffffff" Offset="0"/>
<GradientStop Color="#00ffffff" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Border BorderBrush="#ccffffff" BorderThickness="1">
<Border BorderBrush="#77ffffff" BorderThickness="1"/>
</Border>
<Rectangle x:Name="SelectionHighlight" Fill="Red" Opacity="0"/>
<Rectangle x:Name="MouseOverHighlight" Fill="White" Opacity="0"/>
<TextBlock HorizontalAlignment="Center" Text="{TemplateBinding FormattedDependentValue}" Foreground="White" FontWeight="Bold" Margin="2"/>
</Grid>
and the result is like
and this is what I want to achieve :
the black text shown at the top is the sum of the values of each chart.
Is there any method could do this? Thanks!

ComboBox does not show selected item

I do not get selected item displayed in ComboBox ever since I have overwritten the default style with this one:
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Blue" Offset="0.0"/>
<GradientStop Color="White" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="White" />
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="1" Background="#5089ba" BorderThickness="1" BorderBrush="White"/>
<Border Grid.Column="0" CornerRadius="1" Margin="1" Background="{StaticResource WindowBackgroundBrush}" BorderThickness="0,0,1,0" />
<Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" Fill="White" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
</Grid>
</ControlTemplate>
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
</ToggleButton>
<ContentPresenter Name="ContentSite" IsHitTestVisible="True" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" Background="{StaticResource WindowBackgroundBrush}" BorderThickness="1"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
</Style.Triggers>
</Style>
Selection still works fine and does what it is supposed to do. It is just that I don't get the selected item displayed in header. What could be the reason?
This solved the problem:
<ContentPresenter Name="ContentSite" IsHitTestVisible="True" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}"/>
I was missing the content template binding.
<ContentPresenter Name="ContentSite" IsHitTestVisible="True" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding ComboBox.SelectionBoxItem}"
ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"/>
Now it will work ;)
IT's in your Content presenter.
You need to add a textblock.
<ContentPresenter Name="ContentSite" IsHitTestVisible="True" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text=" " />
</ContentPresenter >

WPF Button pixelated on one site

Every button with the following style is pixelated at the right site. You can see it clearly in the selected (blue) line:
This is the part of my code which causes the problem:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border>
<Border.Background>
<VisualBrush>
<VisualBrush.Visual>
<Grid Width="80" Height="20">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Grid.RowSpan="2" RadiusX="13" RadiusY="13">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="LightGray" Offset="0"/>
<GradientStop Color="Gray" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Margin="3,2" RadiusX="8" RadiusY="12">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#dfff" Offset="0"/>
<GradientStop Color="#0fff" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
<ContentPresenter
x:Name="contentPresenter"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
Does anyone have an idea why the buttons are pixelated on the right site?
I don't think the issue is in your Button style. Here's what your style looks like in my project:
Your buttons might be inheriting style from other templates? (rectangle, border, etc)

Datepicker that is always open

I'm trying to customize the WPF datepicker so that the user doesn't have to click a button to open the picker but that it is always visible.
I've set the PART_Popup to IsOpen = true and StaysOpen = true but that doesn't do the trick..
<Window x:Class="WPFDatePickerSample.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<Style TargetType="DatePicker">
<Setter Property="Foreground" Value="#FF333333" />
<Setter Property="IsTodayHighlighted" Value="True" />
<Setter Property="SelectedDateFormat" Value="Short" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Padding" Value="3,2,0,2"/>
<!--<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint=".5,0" StartPoint=".5,1">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>-->
<Setter Property="BorderThickness" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DatePicker">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}">
<Border.Child>
<Grid x:Name="PART_Root"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid.Resources>
<!-- Main DatePicker Brushes -->
<SolidColorBrush x:Key="DisabledBrush" Color="#A5FFFFFF" />
<!-- Button Template -->
<ControlTemplate x:Key="DropDownButtonTemplate" TargetType="Button">
<Grid>
<!--Start UI-->
<Grid Height="20" Width="20" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" Background="#11FFFFFF" FlowDirection="LeftToRight">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="23*"/>
<RowDefinition Height="19*"/>
<RowDefinition Height="19*"/>
<RowDefinition Height="19*"/>
</Grid.RowDefinitions>
<Border Margin="-1" Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="4" BorderThickness="1" BorderBrush="#FF45D6FA" Opacity="0" CornerRadius="0,0,1,1" x:Name="Highlight"/>
<Border x:Name="Background" Margin="0,-1,0,0" Grid.ColumnSpan="4" Grid.Row="1" Grid.RowSpan="3" BorderThickness="1" BorderBrush="#FFFFFFFF" Opacity="1" CornerRadius=".5" Background="#FF1F3B53"/>
<Border x:Name="BackgroundGradient" Margin="0,-1,0,0" Grid.ColumnSpan="4" Grid.Row="1" Grid.RowSpan="3" BorderThickness="1" BorderBrush="#BF000000" Opacity="1" CornerRadius=".5">
<Border.Background>
<LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1">
<GradientStop Color="#FFFFFFFF" Offset="0" />
<GradientStop Color="#F9FFFFFF" Offset="0.375" />
<GradientStop Color="#E5FFFFFF" Offset="0.625" />
<GradientStop Color="#C6FFFFFF" Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Rectangle Grid.ColumnSpan="4" Grid.RowSpan="1" StrokeThickness="1">
<Rectangle.Stroke>
<LinearGradientBrush EndPoint="0.48,-1" StartPoint="0.48,1.25">
<GradientStop Color="#FF494949"/>
<GradientStop Color="#FF9F9F9F" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Stroke>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.3,-1.1" StartPoint="0.46,1.6">
<GradientStop Color="#FF4084BD"/>
<GradientStop Color="#FFAFCFEA" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Path HorizontalAlignment="Center" Margin="4,3,4,3" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Grid.Column="0" Grid.Row="1" Fill="#FF2F2F2F" Stretch="Fill" Data="M11.426758,8.4305077 L11.749023,8.4305077 L11.749023,16.331387 L10.674805,16.331387 L10.674805,10.299648 L9.0742188,11.298672 L9.0742188,10.294277 C9.4788408,10.090176 9.9094238,9.8090878 10.365967,9.4510155 C10.82251,9.0929432 11.176106,8.7527733 11.426758,8.4305077 z M14.65086,8.4305077 L18.566387,8.4305077 L18.566387,9.3435936 L15.671368,9.3435936 L15.671368,11.255703 C15.936341,11.058764 16.27293,10.960293 16.681133,10.960293 C17.411602,10.960293 17.969301,11.178717 18.354229,11.615566 C18.739157,12.052416 18.931622,12.673672 18.931622,13.479336 C18.931622,15.452317 18.052553,16.438808 16.294415,16.438808 C15.560365,16.438808 14.951641,16.234707 14.468243,15.826504 L14.881817,14.929531 C15.368796,15.326992 15.837872,15.525723 16.289043,15.525723 C17.298809,15.525723 17.803692,14.895514 17.803692,13.635098 C17.803692,12.460618 17.305971,11.873379 16.310528,11.873379 C15.83071,11.873379 15.399232,12.079271 15.016094,12.491055 L14.65086,12.238613 z" Grid.ColumnSpan="4" Grid.RowSpan="3"/>
<Ellipse HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFFFFFFF" StrokeThickness="0" Grid.ColumnSpan="4" Width="3" Height="3"/>
<Border Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="4" BorderThickness="1" BorderBrush="#B2FFFFFF" Opacity="0" CornerRadius="0,0,.5,.5" x:Name="DisabledVisual"/>
</Grid>
<!--End UI-->
</Grid>
</ControlTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox x:Name="PART_TextBox"
Grid.Row="0" Grid.Column="0"
Foreground="{TemplateBinding Foreground}"
BorderThickness="1"
Text="Selecteer datum" MinHeight="20"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center" >
</TextBox>
<Grid x:Name="PART_DisabledVisual"
Opacity="0"
IsHitTestVisible="False"
Grid.Row="0" Grid.Column="0"
Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" RadiusX="1" RadiusY="1" Fill="#A5FFFFFF"/>
<Rectangle Grid.Row="0" Grid.Column="1" RadiusX="1" RadiusY="1" Fill="#A5FFFFFF" Height="18" Width="19" Margin="3,0,3,0" />
<Popup x:Name="PART_Popup"
PlacementTarget="{Binding ElementName=PART_TextBox}"
Placement="Bottom"
IsOpen="True"
StaysOpen="True"
AllowsTransparency="True" />
</Grid>
</Grid>
</Border.Child>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<DatePicker></DatePicker>
</Grid>
I used Calendar instead of DatePicker to achieve my goal.
You have direct Dependency property in DatePicker for this. Please make use of the property in IsDropDownOpen = true. Hope your question is answered.
<DatePicker IsDropDownOpen="True" ></DatePicker>
Regards,
Abdul Rahman A.H.

Different look in design-mode and at runtime

I have small problem with WPF.
Here is my Style where I created a template for DataGrid Column Header.
<Window.Resources>
<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
<Microsoft_Windows_Themes:DataGridHeaderBorder.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF7EE4FF" Offset="0"/>
<GradientStop Color="#FF66D6F3" Offset="1"/>
<GradientStop Color="#FF0097BE" Offset="0.5"/>
</LinearGradientBrush>
</Microsoft_Windows_Themes:DataGridHeaderBorder.Background>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Microsoft_Windows_Themes:DataGridHeaderBorder>
</Grid>
</ControlTemplate>
</Setter.Value>
Here's my DataGrid view control in Grid:
<DataGrid Margin="46.667,41.333,102,87">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Status" Width="*" HeaderStyle=" {DynamicResource DataGridColumnHeaderStyle1}"/>
</DataGrid.Columns>
</DataGrid>
The problem is I can see modified header in VS designer, or in Blend, but when I run my application header style is default. Can anyone help? Thanks in advance.
EDIT
Just found interesting thing. When I put Style in
DataGridTemplateColumn.HeaderStyle
it's working
<DataGrid Margin="46.667,41.333,102,87">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Status" Width="*">
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
<Microsoft_Windows_Themes:DataGridHeaderBorder.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF7EE4FF" Offset="0"/>
<GradientStop Color="#FF66D6F3" Offset="1"/>
<GradientStop Color="#FF0097BE" Offset="0.5"/>
</LinearGradientBrush>
</Microsoft_Windows_Themes:DataGridHeaderBorder.Background>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Microsoft_Windows_Themes:DataGridHeaderBorder>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.HeaderStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Why does it make a difference?
It looks like the DynamicResource is not resolving correctly at runtime. Try switching your DynamicResource to a StaticResource like so:
<DataGridTemplateColumn Header="Status" Width="*"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle1}"/>
For more details on the differences between DynamicResource and StaticResource, see this other StackOverflow question: What's the difference between StaticResource and DynamicResource in WPF?

Resources