How to insert checkbox to the left of the treeviewitem (expander), so it was the same item.
Sergo, Here is a version using the Control Template and we have Checkboxes being placed in column 0 of the item. They should align to the left with the TreeViewItem to the right. The 'magic' piece is in the CheckboxTreeItem style where we add a WrapPanel and place it in Grid.Column='0'.
<Window
x:Class="TreeViewHeaderTest.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="GlyphBrush"
Color="#444" />
<Style
x:Key="ExpandCollapseToggleStyle"
TargetType="ToggleButton">
<Setter
Property="Focusable"
Value="False" />
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="ToggleButton">
<WrapPanel
Background="Transparent">
<Path
x:Name="ExpandPath"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="1,1,1,1"
Fill="{StaticResource GlyphBrush}"
Data="M 4 0 L 8 4 L 4 8 Z" />
</WrapPanel>
<ControlTemplate.Triggers>
<Trigger
Property="IsChecked"
Value="True">
<Setter
Property="Data"
TargetName="ExpandPath"
Value="M 0 4 L 8 4 L 4 8 Z" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style
x:Key="TreeViewItemFocusVisual">
<Setter
Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle
Margin="0,0,0,0"
StrokeThickness="5"
Stroke="Black"
StrokeDashArray="1 2"
Opacity="0" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style
x:Key="CheckboxTreeItem"
TargetType="{x:Type TreeViewItem}">
<Setter
Property="IsExpanded"
Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter
Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter
Property="Background"
Value="Transparent" />
<Setter
Property="HorizontalContentAlignment"
Value="{Binding Path=HorizontalContentAlignment,
RelativeSource={RelativeSource
AncestorType={x:Type ItemsControl}}}" />
<Setter
Property="VerticalContentAlignment"
Value="{Binding Path=VerticalContentAlignment,
RelativeSource={RelativeSource
AncestorType={x:Type ItemsControl}}}" />
<Setter
Property="Padding"
Value="1,0,0,0" />
<Setter
Property="Foreground"
Value="{StaticResource {x:Static
SystemColors.ControlTextBrushKey}}" />
<Setter
Property="FocusVisualStyle"
Value="{StaticResource TreeViewItemFocusVisual}" />
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition
MinWidth="19"
Width="Auto" />
<ColumnDefinition
Width="Auto" />
<ColumnDefinition
Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<WrapPanel
Grid.Column='0'>
<CheckBox />
<ToggleButton
x:Name="Expander"
Style="{StaticResource
ExpandCollapseToggleStyle}"
IsChecked="{Binding Path=IsExpanded,
RelativeSource={RelativeSource
TemplatedParent}}"
ClickMode="Press" />
</WrapPanel>
<Border
Name="Bd"
Grid.Column="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding
BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter
x:Name="PART_Header"
ContentSource="Header"
HorizontalAlignment="{TemplateBinding
HorizontalContentAlignment}" />
</Border>
<ItemsPresenter
x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2" />
</Grid>
<ControlTemplate.Triggers>
<Trigger
Property="IsExpanded"
Value="false">
<Setter
TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed" />
</Trigger>
<Trigger
Property="HasItems"
Value="false">
<Setter
TargetName="Expander"
Property="Visibility"
Value="Hidden" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition
Property="HasHeader"
Value="false" />
<Condition
Property="Width"
Value="Auto" />
</MultiTrigger.Conditions>
<Setter
TargetName="PART_Header"
Property="MinWidth"
Value="75" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition
Property="HasHeader"
Value="false" />
<Condition
Property="Height"
Value="Auto" />
</MultiTrigger.Conditions>
<Setter
TargetName="PART_Header"
Property="MinHeight"
Value="19" />
</MultiTrigger>
<Trigger
Property="IsSelected"
Value="true">
<Setter
TargetName="Bd"
Property="Background"
Value="AliceBlue" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition
Property="IsSelected"
Value="true" />
<Condition
Property="IsSelectionActive"
Value="false" />
</MultiTrigger.Conditions>
<Setter
TargetName="Bd"
Property="Background"
Value="{StaticResource {x:Static
SystemColors.ControlBrushKey}}" />
<Setter
Property="Foreground"
Value="{StaticResource {x:Static
SystemColors.ControlTextBrushKey}}" />
</MultiTrigger>
<Trigger
Property="IsEnabled"
Value="false">
<Setter
Property="Foreground"
Value="{StaticResource {x:Static
SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<TreeView>
<TreeViewItem
Style='{StaticResource CheckboxTreeItem}'
Header='Item'
IsExpanded='True'>
<TreeViewItem
Style='{StaticResource CheckboxTreeItem}'
Header='SubItem 1' />
<TreeViewItem
Style='{StaticResource CheckboxTreeItem}'
Header='SubItem 2'>
<TreeViewItem
Style='{StaticResource CheckboxTreeItem}'
Header='SubItem a' />
<TreeViewItem
Style='{StaticResource CheckboxTreeItem}'
Header='SubItem b' />
</TreeViewItem>
</TreeViewItem>
</TreeView>
</Window>
You can modify the header of your TreeViewItem to add a Checkbox to the left. Here's a quick example to get you started, this xaml just adds a checkbox to the left and a TextBlock to the right.
<TreeView>
<TreeViewItem>
<TreeViewItem.Header>
<WrapPanel>
<CheckBox />
<TextBlock
Margin='5,0'
Text='Item' />
</WrapPanel>
</TreeViewItem.Header>
<TreeViewItem>
<TreeViewItem.Header>
<WrapPanel>
<CheckBox />
<TextBlock
Margin='5,0'
Text='SubItem' />
</WrapPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeViewItem>
</TreeView>
Depending on your scenario you may want to create a whole new template to override the default look for all TreeViewItems, if you do, then checkout the MSDN TreeViewItem Control Template example:
http://msdn.microsoft.com/en-us/library/ms788727.aspx
Related
How does one set the style of a sub-menu under a ContextMenu?
Here is the code for ContextMenu:
<ListView.ContextMenu>
<fw:AcrylicContextMenu Style="{StaticResource NowPlayingContextMenuStyle}" ItemContainerStyle="{StaticResource NowPlayingContextMenuItemStyle}">
<MenuItem>
<MenuItem.Header>
<TextBlock Text="Play" Style="{StaticResource NowPlayingContextMenuItemHeader}" />
</MenuItem.Header>
<MenuItem.Icon>
<materialDesign:PackIcon Kind="Play" />
</MenuItem.Icon>
</MenuItem>
<!-- ... -->
<MenuItem>
<MenuItem.Header>
<TextBlock Text="Sort" Style="{StaticResource NowPlayingContextMenuItemHeader}" />
</MenuItem.Header>
<MenuItem.Icon>
<materialDesign:PackIcon Kind="Sort" />
</MenuItem.Icon>
<!--#region SubMenu Sort-->
<MenuItem>
<MenuItem.Header>
<TextBlock Text="Ascending" Style="{StaticResource NowPlayingContextMenuItemHeader}" />
</MenuItem.Header>
<MenuItem.Icon>
<materialDesign:PackIcon Kind="SortAscending" />
</MenuItem.Icon>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<TextBlock Text="Descending" Style="{StaticResource NowPlayingContextMenuItemHeader}" />
</MenuItem.Header>
<MenuItem.Icon>
<materialDesign:PackIcon Kind="SortDescending" />
</MenuItem.Icon>
</MenuItem>
<!--#endregion SubMenu Sort-->
</MenuItem>
</fw:AcrylicContextMenu>
</ListView.ContextMenu>
The styles have been defined in this way:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:V_Player.Resources.Styles"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes">
<Style x:Key="NowPlayingListViewItemStyle" BasedOn="{StaticResource MaterialDesignListBoxItem}" TargetType="ListViewItem">
<Setter Property="Height" Value="32" />
<Setter Property="Padding" Value="8,8,8,8" />
</Style>
<Style x:Key="NowPlayingContextMenuStyle" BasedOn="{StaticResource MaterialDesignMenu}" TargetType="ContextMenu">
<Setter Property="Foreground" Value="White" />
<Setter Property="Opacity" Value="0.7" />
<Setter Property="FontSize" Value="14" />
<Setter Property="MinWidth" Value="128" />
</Style>
<Style x:Key="NowPlayingContextMenuItemStyle" BasedOn="{StaticResource MaterialDesignMenuItem}" TargetType="MenuItem">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="8, 0, 8, 0" />
</Style>
<Style x:Key="NowPlayingContextMenuSeparatorStyle" BasedOn="{StaticResource MaterialDesignSeparator}" TargetType="Separator">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="Height" Value="1" />
</Style>
<Style x:Key="NowPlayingContextMenuItemHeader" BasedOn="{StaticResource MaterialDesignTextBlock}" TargetType="TextBlock">
<Setter Property="Margin" Value="-12, 0, 0, 0" />
</Style>
</ResourceDictionary>
And results are not quite right... For sub menus of course.
Menu background is Acrylic and transparent, but sub menu no, it is gray and has material design style.
What can i do for fix it?
This style work for me:
<!-- Separator -->
<Style x:Key="SeparatorStyle" TargetType="{x:Type Separator}">
<Setter Property="Height" Value="1" />
<Setter Property="Background" Value="#0f3c5a" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Separator}">
<Rectangle Height="{TemplateBinding Height}" Fill="White" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Outer menu items -->
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Background" Value="{StaticResource ToolBarBackground}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="5"/>
<Style.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" Value="Black" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="LightGray" />
</Trigger>
</Style.Triggers>
</Style>
<!-- Outer menu -->
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Grid.IsSharedSizeScope" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<!-- Here is where you change the border thickness to zero on the menu -->
<Border
x:Name="Border"
BorderThickness="0">
<StackPanel
ClipToBounds="True"
IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Cycle"
Orientation="Vertical" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasDropShadow" Value="true">
<Setter TargetName="Border" Property="Padding" Value="0,3,0,3" />
<Setter TargetName="Border" Property="CornerRadius" Value="4" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource ToolBarBackground}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- SubmenuItem -->
<ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
<Border Name="Border">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
<ColumnDefinition Width="13" />
</Grid.ColumnDefinitions>
<ContentPresenter
Name="Icon"
Margin="6,0,6,0"
VerticalAlignment="Center"
ContentSource="Icon" />
<Border
Name="Check"
Width="13"
Height="13"
Margin="6,0,6,0"
Background="{StaticResource ToolBarBackground}"
BorderBrush="#5082a4"
BorderThickness="1"
Visibility="Collapsed">
<Path
Name="CheckMark"
Width="7"
Height="7"
Data="M 0 0 L 7 7 M 0 7 L 7 0"
SnapsToDevicePixels="False"
Stroke="#5082a4"
StrokeThickness="2"
Visibility="Hidden" />
</Border>
<ContentPresenter
Name="HeaderHost"
Grid.Column="1"
ContentSource="Header"
RecognizesAccessKey="True" />
<TextBlock
x:Name="InputGestureText"
Grid.Column="2"
Margin="5,2,0,2"
DockPanel.Dock="Right"
Text="{TemplateBinding InputGestureText}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Icon" Value="{x:Null}">
<Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="CheckMark" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsCheckable" Value="true">
<Setter TargetName="Check" Property="Visibility" Value="Visible" />
<Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
</Trigger>
<Trigger Property="IsHighlighted" Value="true">
<Setter TargetName="Border" Property="Background" Value="#5082a4" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#0f3c5a" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!-- SubmenuHeader -->
<ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
<Border Name="Border">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
<ColumnDefinition Width="13" />
</Grid.ColumnDefinitions>
<ContentPresenter
Name="Icon"
Margin="6,0,6,0"
VerticalAlignment="Center"
ContentSource="Icon" />
<ContentPresenter
Name="HeaderHost"
Grid.Column="1"
ContentSource="Header"
RecognizesAccessKey="True" />
<TextBlock
x:Name="InputGestureText"
Grid.Column="2"
Margin="5,2,2,2"
DockPanel.Dock="Right"
Text="{TemplateBinding InputGestureText}" />
<Path
Grid.Column="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 0 7 L 4 3.5 Z"
Fill="#0f3c5a" />
<Popup
Name="Popup"
AllowsTransparency="True"
Focusable="False"
HorizontalOffset="-4"
IsOpen="{TemplateBinding IsSubmenuOpen}"
Placement="Right"
PopupAnimation="Fade">
<Border
Name="SubmenuBorder"
Background="{StaticResource ToolBarBackground}"
BorderBrush="{StaticResource ToolBarBackground}"
BorderThickness="1"
SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Icon" Value="{x:Null}">
<Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="IsHighlighted" Value="true">
<Setter TargetName="Border" Property="Background" Value="#5082a4" />
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
<Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4" />
<Setter TargetName="SubmenuBorder" Property="Padding" Value="0,3,0,3" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#0f3c5a" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
I have a custom WPF combobox, on top I have textbox and below item template which will display the list of items. Now when I move mouse over the items then the respecting is getting selected, but without clicking on any item just clicking into textbox which is present inside same combobox. Now I want the selection from combobox to be cleared. Because when I type something in textbox and click on enter key then it is considering the selected item as last highlighted one. But actually if I click on textbox and typed something then i want to do different process.
<Style x:Key="MyCustomComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="{StaticResource myWhite}" />
<Setter Property="BorderBrush" Value="{StaticResource myBlue}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="Padding" Value="16,5,10,0" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<UniformGrid Columns="2" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="MainGrid" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="0" MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" />
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup"
Grid.ColumnSpan="2"
Margin="1"
AllowsTransparency="true"
IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
Placement="Bottom"
PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
<Themes:SystemDropShadowChrome x:Name="Shdw"
Width="Auto"
MaxHeight="{TemplateBinding MaxDropDownHeight}"
CornerRadius="5"
Color="Transparent">
<Border x:Name="DropDownBorder"
Padding="0,1,0,0"
Background="White"
BorderBrush="#92AEC4"
BorderThickness="1"
CornerRadius="5">
<Grid Width="300" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0"
Width="250"
Margin="8,4,8,16">
<Border Height="24"
BorderBrush="#656565"
BorderThickness="1"
CornerRadius="3">
<StackPanel Margin="8,0" Orientation="Horizontal">
<TextBlock Padding="0,0,5,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="ACE-Icon-Font"
FontSize="15"
Foreground="#656565"
Text="" />
<Grid>
<TextBox x:Name="SearchTextBox"
Width="{Binding ElementName=DropDownBorder, Path=ActualWidth}"
VerticalAlignment="Center"
Foreground="#333333"
IsEnabled="True"
Focusable="True"
Text="{Binding Path=CustNameSearch, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="BorderThickness" Value="0" />
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</StackPanel>
</Border>
</Grid>
<Grid Grid.Row="1"
Background="Transparent"
RenderOptions.ClearTypeHint="Enabled">
<ScrollViewer x:Name="DropDownScrollViewer"
MaxHeight="194"
Template="{DynamicResource ScrollViewerControlTemplate1}">
<Grid>
<Canvas Width="0"
Height="0"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Rectangle x:Name="OpaqueRect"
Grid.Column="1"
Width="{Binding ActualWidth, ElementName=DropDownBorder}"
Height="{Binding ActualHeight, ElementName=DropDownScrollViewer}"
Fill="{Binding Background, ElementName=DropDownBorder}" />
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter"
KeyboardNavigation.DirectionalNavigation="Contained"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Button Margin="16,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding Path=HandleCommandsCommand}"
CommandParameter="AddCustDetails"
Content="Create Customer"
Visibility="{Binding Path=CreateCustButtonVisibility, FallbackValue=Collapsed}">
</Button>
</Grid>
</ScrollViewer>
</Grid>
</Grid>
</Border>
</Themes:SystemDropShadowChrome>
</Popup>
<ToggleButton x:Name="TogB"
Grid.ColumnSpan="2"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource ComboBoxReadonlyToggleButton}" />
<ContentPresenter x:Name="ConPres"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="Select Cust.."
ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
IsHitTestVisible="false"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.Style>
<Style>
<Setter Property="TextBlock.Foreground" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TogB, Path=IsChecked}" Value="True">
<Setter Property="TextBlock.Foreground" Value="#FFFFFF" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="PART_Popup" Property="HasDropShadow" Value="true">
<Setter TargetName="Shdw" Property="Margin" Value="0,0,3,3" />
<Setter TargetName="Shdw" Property="Color" Value="#16000000" />
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="Height" Value="120" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="ConPres" Property="TextBlock.Foreground" Value="#CBCBCB" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="true" />
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</MultiTrigger>
<Trigger SourceName="DropDownScrollViewer" Property="ScrollViewer.CanContentScroll" Value="false">
<Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}" />
<Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="Padding" Value="3" />
<Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
If user click on textbox inside to combobox then the selection from combobox should clear. Or EnterKey pressed on textbox then it should not select anything form combobox and should remain same.
Hi I need to change the look of RibbonMenuButton, but I am not able to find its style, part or template on internet. Neither I am able to generate a copy from blend or vs designer. So if anyone has its control template please share
You can use ILSpy (an open-source .NET assembly browser and decompiler) for exploring an assembly and - in your case - for exploring resources that contain - for example - a style.
By using this tool, you will find that the generic style of a RibbonMenuButton in the RibbonControlsLibrary.dll (version 4.0.0.11019) is (I cut and past it for your convenience):
<Style x:Key="{x:Type ribbon:RibbonMenuButton}" TargetType="{x:Type ribbon:RibbonMenuButton}">
<Style.Resources>
<Style TargetType="{x:Type ribbon:RibbonSeparator}">
<Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True" />
<Setter Property="UIElement.Focusable" Value="False" />
<Setter Property="Control.BorderBrush" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.BorderBrush}" />
<Setter Property="Control.Background" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.Background}" />
<Setter Property="Control.FontWeight" Value="Bold" />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ribbon:RibbonSeparator}">
<Border Name="MainBorder" SnapsToDevicePixels="True" BorderThickness="0,1,0,1" BorderBrush="{TemplateBinding Control.BorderBrush}" Background="{TemplateBinding Control.Background}">
<Border Name="Overlay" Background="{StaticResource ä}">
<TextBlock Name="Text" Margin="2,1,2,1" Text="{TemplateBinding ribbon:RibbonSeparator.Label}" />
</Border>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Value="True" Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}">
<Setter TargetName="Text" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" Property="TextBlock.Foreground" />
<Setter TargetName="Overlay" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="MainBorder" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="MainBorder" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Property="Border.Background" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ribbon:RibbonSeparator.Label" Value="{x:Null}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ribbon:RibbonSeparator}">
<Grid Name="Grid" SnapsToDevicePixels="True" Margin="1">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="22" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="14" />
</Grid.ColumnDefinitions>
<Border Name="SideBarBorder" BorderThickness="0,0,1,0" Margin="0,-1,0,-1" Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}">
<Border Name="SideBarOverlay" Background="{StaticResource ä}" />
</Border>
<Line Name="Line" Grid.Column="1" Grid.ColumnSpan="2" Margin="2,0,0,0" X1="0" Y1="0" X2="1" Y2="0" Stroke="{TemplateBinding Control.BorderBrush}" StrokeThickness="1" Stretch="Fill" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Value="True" Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}">
<Setter TargetName="SideBarBorder" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="SideBarBorder" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="SideBarOverlay" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="Line" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Shape.Stroke" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
<Setter Property="Control.Background" Value="#00FFFFFF" />
<Setter Property="Control.BorderBrush" Value="#00FFFFFF" />
<Setter Property="Control.BorderThickness" Value="1" />
<Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Control.HorizontalContentAlignment" Value="Center" />
<Setter Property="Control.Padding" Value="2,0,2,0" />
<Setter Property="ribbon:RibbonMenuButton.MouseOverBorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.MouseOverBorderBrush}" />
<Setter Property="ribbon:RibbonMenuButton.MouseOverBackground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.MouseOverBackground}" />
<Setter Property="ribbon:RibbonMenuButton.PressedBorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.PressedBorderBrush}" />
<Setter Property="ribbon:RibbonMenuButton.PressedBackground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.PressedBackground}" />
<Setter Property="ribbon:RibbonMenuButton.FocusedBorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.FocusedBorderBrush}" />
<Setter Property="ribbon:RibbonMenuButton.FocusedBackground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.FocusedBackground}" />
<Setter Property="ToolTipService.InitialShowDelay" Value="{StaticResource à}" />
<Setter Property="ToolTipService.ShowDuration" Value="{StaticResource á}" />
<Setter Property="ToolTipService.BetweenShowDelay" Value="{StaticResource â}" />
<Setter Property="ribbon:RibbonTwoLineText.PathData" Value="{StaticResource ÿ}" />
<Setter Property="ribbon:RibbonMenuButton.QuickAccessToolBarControlSizeDefinition">
<Setter.Value>
<rcp:RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="False" />
</Setter.Value>
</Setter>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ribbon:RibbonMenuButton}">
<Grid Name="MainGrid" SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<rcp:RibbonToggleButton x:Name="PART_ToggleButton" Template="{StaticResource Ĥ}" RibbonTwoLineText.PathData="{TemplateBinding ribbon:RibbonTwoLineText.PathData}" Label="{TemplateBinding ribbon:RibbonMenuButton.Label}" LargeImageSource="{TemplateBinding ribbon:RibbonMenuButton.LargeImageSource}" SmallImageSource="{TemplateBinding ribbon:RibbonMenuButton.SmallImageSource}" ControlSizeDefinition="{TemplateBinding ribbon:RibbonMenuButton.ControlSizeDefinition}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Background="{TemplateBinding Control.Background}" CornerRadius="2" MouseOverBorderBrush="{TemplateBinding ribbon:RibbonMenuButton.MouseOverBorderBrush}" MouseOverBackground="{TemplateBinding ribbon:RibbonMenuButton.MouseOverBackground}" CheckedBorderBrush="{TemplateBinding ribbon:RibbonMenuButton.PressedBorderBrush}" CheckedBackground="{TemplateBinding ribbon:RibbonMenuButton.PressedBackground}" FocusedBorderBrush="{TemplateBinding ribbon:RibbonMenuButton.FocusedBorderBrush}" FocusedBackground="{TemplateBinding ribbon:RibbonMenuButton.FocusedBackground}" HorizontalContentAlignment="{TemplateBinding Control.HorizontalContentAlignment}" Padding="{TemplateBinding Control.Padding}" ClickMode="Press" Style="{x:Null}" FocusVisualStyle="{x:Null}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}">
<rcp:RibbonToggleButton.Resources>
<Thickness Left="1" Top="1" Right="1" Bottom="2">
<ComponentResourceKey.Key TypeInTargetAssembly="{x:Type ribbon:Ribbon}" ResourceId="LargeImageMargin" />
</Thickness>
</rcp:RibbonToggleButton.Resources>
</rcp:RibbonToggleButton>
<Popup Name="PART_Popup" HorizontalOffset="1" VerticalOffset="-1" AllowsTransparency="True" Placement="Bottom" Focusable="False" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen}">
<sdsc:SystemDropShadowChrome Name="Shadow" Color="Transparent" p15:KeyTipService.IsKeyTipScope="True" RenderOptions.ClearTypeHint="Enabled" xmlns:p15="clr-namespace:Microsoft.Windows.Controls;assembly=RibbonControlsLibrary,Version=4.0.0.11019,Culture=neutral,PublicKeyToken=31bf3856ad364e35">
<Border Name="MenuBorder" BorderThickness="{TemplateBinding Control.BorderThickness}" CornerRadius="2" BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Ribbon.BorderBrush}" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Ribbon.Background}">
<Border Name="SubMenuInnerBorder" Background="{StaticResource ï}" BorderThickness="0" CornerRadius="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer Name="PART_SubMenuScrollViewer" Grid.Row="1" Margin="1" Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas Name="BackgroundCanvas" Height="0" Width="0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle Name="OpaqueRect" RadiusX="2" RadiusY="2" Height="{Binding ElementName=MenuBorder, Path=ActualHeight}" Width="{Binding ElementName=MenuBorder, Path=ActualWidth}" Fill="{Binding ElementName=MenuBorder, Path=Background}" />
<Rectangle Name="OverlayRect" RadiusX="2" RadiusY="2" Height="{Binding ElementName=SubMenuInnerBorder, Path=ActualHeight}" Width="{Binding ElementName=SubMenuInnerBorder, Path=ActualWidth}" Fill="{Binding ElementName=SubMenuInnerBorder, Path=Background}" />
</Canvas>
<ItemsPresenter Name="ItemsPresenter" KeyboardNavigation.TabNavigation="Cycle" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" />
</Grid>
</ScrollViewer>
<Border Name="ResizeControl" Grid.Row="2" Visibility="Collapsed" Background="{StaticResource æ}" BorderThickness="0,1,0,0" BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Ribbon.BorderBrush}">
<Thumb Name="PART_ResizeThumb" Style="{StaticResource ç}" />
</Border>
</Grid>
</Border>
</Border>
</sdsc:SystemDropShadowChrome>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="PART_SubMenuScrollViewer" Property="ScrollViewer.CanContentScroll" Value="False">
<Setter TargetName="OpaqueRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=VerticalOffset}" Property="Canvas.Top" />
<Setter TargetName="OpaqueRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=HorizontalOffset}" Property="Canvas.Left" />
<Setter TargetName="OverlayRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=VerticalOffset}" Property="Canvas.Top" />
<Setter TargetName="OverlayRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=HorizontalOffset}" Property="Canvas.Left" />
</Trigger>
<DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsInQuickAccessToolBar}">
<Setter TargetName="PART_ToggleButton" Property="FrameworkElement.Height" Value="Auto" />
</DataTrigger>
<Trigger Property="UIElement.IsEnabled" Value="False">
<Setter TargetName="MainGrid" Value="{StaticResource Ë}" Property="TextElement.Foreground" />
</Trigger>
<Trigger Property="ribbon:RibbonMenuButton.HasGallery" Value="True">
<Setter TargetName="PART_SubMenuScrollViewer" Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter TargetName="PART_SubMenuScrollViewer" Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeHorizontally" Value="True" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeVertically" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="ResizeControl" Property="UIElement.Visibility" Value="Visible" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeHorizontally" Value="False" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeVertically" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="ResizeControl" Property="UIElement.Visibility" Value="Visible" />
<Setter TargetName="PART_ResizeThumb" Value="{StaticResource è}" Property="FrameworkElement.Style" />
</MultiTrigger>
<Trigger Property="ribbon:RibbonMenuButton.IsDropDownPositionedAbove" Value="True">
<Setter TargetName="ResizeControl" Property="Grid.Row" Value="0" />
<Setter TargetName="ResizeControl" Property="Border.BorderThickness" Value="0,0,0,1" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ribbon:RibbonMenuButton.IsDropDownPositionedAbove" Value="True" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeHorizontally" Value="True" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeVertically" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_ResizeThumb" Value="{StaticResource é}" Property="FrameworkElement.Style" />
</MultiTrigger>
<Trigger Property="ItemsControl.HasItems" Value="False">
<Setter TargetName="PART_ToggleButton" Value="{x:Null}" Property="ribbon:RibbonTwoLineText.PathData" />
</Trigger>
<Trigger SourceName="PART_Popup" Property="Popup.HasDropShadow" Value="True">
<Setter TargetName="Shadow" Property="FrameworkElement.Margin" Value="0,0,5,5" />
<Setter TargetName="Shadow" Value="{StaticResource Õ}" Property="classic:SystemDropShadowChrome.Color" />
</Trigger>
<Trigger SourceName="PART_Popup" Property="Popup.IsOpen" Value="False">
<Setter TargetName="PART_Popup" Property="Popup.PopupAnimation" Value="None" />
</Trigger>
<DataTrigger Value="True" Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}">
<Setter TargetName="BackgroundCanvas" Property="UIElement.Visibility" Value="Collapsed" />
<Setter TargetName="Shadow" Property="classic:SystemDropShadowChrome.Color" Value="Transparent" />
<Setter TargetName="PART_Popup" Property="Popup.PopupAnimation" Value="None" />
<Setter TargetName="PART_SubMenuScrollViewer" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" Property="Control.Foreground" />
<Setter TargetName="MenuBorder" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="MenuBorder" Value="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" Property="Border.Background" />
<Setter TargetName="MenuBorder" Property="Border.CornerRadius" Value="0" />
<Setter TargetName="ResizeControl" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="ResizeControl" Value="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" Property="Border.Background" />
<Setter TargetName="SubMenuInnerBorder" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="SubMenuInnerBorder" Property="Border.CornerRadius" Value="0" />
<Setter TargetName="MainGrid" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="TextElement.Foreground" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="False" />
<Condition Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="MainGrid" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Property="TextElement.Foreground" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ribbon:RibbonMenuButton.IsDropDownOpen" Value="True">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
Anyway I suggest you to browse it by your own, in order to "discover" specific details that you may need to know for your project.
I have a standard Menu with a couple of toplevel MenuItems + children. The controltemplate looks like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}">
<Setter Property="Foreground" Value="{DynamicResource LinkTextColorBrush}" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Menu}">
<Border BorderThickness="0">
<StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
<Border x:Name="Border">
<Grid>
<ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
<Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
<Border x:Name="SubmenuBorder" SnapsToDevicePixels="True" BorderThickness="1" Background="GhostWhite">
<Border.BorderBrush>
<SolidColorBrush Color="{Binding Source={StaticResource CstBorderLightGrey}, Path=Color}" />
</Border.BorderBrush>
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSuspendingPopupAnimation" Value="true">
<Setter TargetName="Popup" Property="PopupAnimation" Value="None" />
</Trigger>
<Trigger Property="IsHighlighted" Value="true">
<Setter Property="Foreground" Value="{DynamicResource HotLinkTextColorBrush}" />
<Setter Property="BorderBrush" TargetName="Border" Value="Transparent" />
<Setter Property="Background" TargetName="Border" Value="White" />
</Trigger>
<Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
<Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4" />
<Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
<Border x:Name="Border">
<Grid>
<ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
<Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
<Border x:Name="SubmenuBorder" SnapsToDevicePixels="True" BorderThickness="1" Background="GhostWhite">
<Border.BorderBrush>
<SolidColorBrush Color="{Binding Source={StaticResource CstBorderLightGrey}, Path=Color}" />
</Border.BorderBrush>
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSuspendingPopupAnimation" Value="true">
<Setter TargetName="Popup" Property="PopupAnimation" Value="None" />
</Trigger>
<Trigger Property="IsHighlighted" Value="true">
<Setter Property="Foreground" Value="{DynamicResource HotLinkTextColorBrush}" />
<Setter Property="BorderBrush" TargetName="Border" Value="Transparent" />
<Setter Property="Background" TargetName="Border" Value="White" />
</Trigger>
<Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
<Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4" />
<Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ResourceDictionary>
There are some minor adjustments left but apart from that it works fine. But i'm having one problem, I need to make the toplevelitems underlined during mouseover. I've been searching for information about this for a long time and i've come up with nothing so far.
You can just use the MenuItem.Header property to add a TextBlock with an underline to the menu item header section:
<MenuItem>
<MenuItem.Header>
<TextBlock Text="{Binding YourHeaderProperty}" TextDecorations="Underline" />
</MenuItem.Header>
</MenuItem>
It's worth pointing out that you may confuse your users when you do this, because underlined text usually means that it is a hyper link.
UPDATE >>>
You just need to rearrange the code a little and add a DataTrigger to make the change:
<MenuItem>
<MenuItem.Header>
<TextBlock Text="rtuwruhey5uje5yu">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextDecorations" Value="None" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={
RelativeSource AncestorType={x:Type MenuItem}}}"
Value="True">
<Setter Property="TextDecorations" Value="Underline" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</MenuItem.Header>
</MenuItem>
Background
I've searched fot a day now, without a good result. What I basically try to achieve is the following treeview:
<Grid>
<TreeView>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel>
<TextBlock Text="Header Content 1"/>
<TextBlock Text="Header Content 2"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel >
<TextBlock Margin="-19,0,0,0" Text="Expandable Content 1"/>
<TextBlock Margin="-19,0,0,0" Text="Expandable Content 2"/>
</StackPanel>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel>
<TextBlock Text="Content Footer"/>
<TextBlock Text="Content Footer"/>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeView>
</Grid>
The Itemspresenter of the treeview should present its content between two content panels (Header & Footer), except that in the code above requires two treeview items and I would like to have it as 1 (selectable) treeview item..
If edited the default Controltemplate of the treeview in an attempt to add a textblock on a third row in a Grid, but for some reason it copies this textblock twice when the treeview item is expanded:
<Window.Resources>
<SolidColorBrush x:Key="GlyphBrush" Color="#444" />
<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid Width="15"
Height="13"
Background="Transparent">
<Path x:Name="ExpandPath"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="1,1,1,1"
Fill="{StaticResource GlyphBrush}"
Data="M 4 0 L 8 4 L 4 8 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" TargetName="ExpandPath" Value="M 0 4 L 8 4 L 4 8 Z"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TreeViewItemFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle Margin="0,0,0,0"
StrokeThickness="5"
Stroke="Black"
StrokeDashArray="1 2"
Opacity="0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="1,0,0,0"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19" Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ToggleButton x:Name="Expander"
Style="{StaticResource ExpandCollapseToggleStyle}"
IsChecked="{Binding Path=IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"/>
<Border Name="Bd"
Grid.Column="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Border>
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="Footer Text"></TextBlock>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="Expander"
Property="Visibility"
Value="Hidden"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasHeader" Value="false"/>
<Condition Property="Width" Value="Auto"/>
</MultiTrigger.Conditions>
<Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasHeader" Value="false"/>
<Condition Property="Height" Value="Auto"/>
</MultiTrigger.Conditions>
<Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
</MultiTrigger>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> </Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TreeView>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel>
<TextBlock Text="Treeview Item 1"/>
<TextBlock Text="Header Content"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel >
<TextBlock Margin="-19,0,0,0" Text="Expandable Content 1"/>
<TextBlock Margin="-19,0,0,0" Text="Expandable Content 2"/>
</StackPanel>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel>
<TextBlock Text="Treeview Item 2"/>
<TextBlock Text="Header Content"/>
</StackPanel>
</TreeViewItem.Header>
<StackPanel >
<TextBlock Margin="-19,0,0,0" Text="Expandable Content 1"/>
<TextBlock Margin="-19,0,0,0" Text="Expandable Content 2"/>
</StackPanel>
</TreeViewItem>
</TreeView>
</Grid>
In the example above the Footer Content is shown twice because it is added to the bottom of the Itemspresenter.
Question
Does anyone know how to solve this? Or perhaps knows a different approach to achieve the same result?
The problem here is the Content of the TreeViewItem gets wrapped in an implicit TreeViewItem by itself which thus gets another "footer" thus making it look like 2 "footer" elements per TreeViewItem.
Confirmed with Snoop:
The TreeViewItem we actually add
and the implicit one created by the Content of TreeViewItem
Work-around:
just apply a name to the "footer" TextBlock such as
<TextBlock x:Name="footer"
Grid.Row="2"
Grid.Column="1"
Text="Footer Text" />
and update the Trigger for HasItems -> False to:
<Trigger Property="HasItems"
Value="false">
<Setter TargetName="Expander"
Property="Visibility"
Value="Hidden" />
<Setter TargetName="footer"
Property="Visibility"
Value="Collapsed" />
</Trigger>
Now the footer text will only be visible when the TreeViewItem has children thereby giving you the appearance your expecting