Change WPF Combobox arrow using a data trigger - wpf

I am using a multi-select combobox that I found on CodeProject [http://www.codeproject.com/Articles/563862/Multi-Select-ComboBox-in-WPF].
I am attempting to change the shape of the arrow depending on whether or not all of the items are selected. However I have been unable to correctly reference the x:Name of Arrow in the setter.
Here is the code I've been trying to modify:
<UserControl x:Class="Z_Sys_UI.System.MultiSelectComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" >
<ComboBox
x:Name="MultiSelectCombo"
SnapsToDevicePixels="True"
OverridesDefaultStyle="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
IsSynchronizedWithCurrentItem="True"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Title}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
Click="CheckBox_Click" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.Template>
<ControlTemplate TargetType="ComboBox">
<Grid >
<ToggleButton
x:Name="ToggleButton"
Grid.Column="2" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
Focusable="false"
ClickMode="Press" HorizontalContentAlignment="Left" >
<ToggleButton.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="18"/>
</Grid.ColumnDefinitions>
<Border
x:Name="Border"
Grid.ColumnSpan="2"
CornerRadius="2"
Background="White"
BorderBrush="Black"
BorderThickness="1,1,1,1" />
<Border
x:Name="BorderComp"
Grid.Column="0"
CornerRadius="2"
Margin="1"
Background="White"
BorderBrush="Black"
BorderThickness="0,0,0,0" >
<TextBlock Text="{Binding Path=Text,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
Background="White" Padding="3" />
</Border>
<Path
x:Name="Arrow"
Grid.Column="1"
Fill="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z">
</Path>
</Grid>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
<Popup
Name="Popup"
Placement="Bottom"
AllowsTransparency="True"
Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}"
PopupAnimation="Slide">
<Grid
Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border
x:Name="DropDownBorder"
BorderThickness="1" Background="White"
BorderBrush="Black"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
<DataTrigger Binding="{Binding Path=SelectAll, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="false" >
<Setter TargetName="Arrow" Property="Data" Value="M 0 0 L 2 2 L 8 0 Z" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
Right now it is giving errors that say The name Arrow is not recognized, and the member Data is not recognized or is not accessible.
It's also possible that the SelectAll path that I'm trying to trigger off of does not work either. The CodeProject code didn't seem to have that property fully implemented. I'll deal with that possibility later.
Thanks in advance for any answers or suggestions.

The name Arrow is out of scope in the ComboBox ControlTemplate, because it's defined in the ToggleButton ControlTemplate. The solution turns out to be easy: Just put the trigger in the template where the target control is in scope.
<ToggleButton.Template>
<ControlTemplate>
<Grid>
<!-- snip snip snip -->
<!-- snip snip snip -->
<!-- snip snip snip -->
<Path
x:Name="Arrow"
Grid.Column="1"
Fill="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"
>
</Path>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger
Binding="{Binding Path=SelectAll, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}"
Value="false" >
<Setter
TargetName="Arrow"
Property="Data"
Value="M 0 0 L 2 2 L 8 0 Z"
/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
I'm assuming that SelectAll is a boolean dependency property, and a member of MultiSelectComboBox, because that seems to be implied by what you're doing with it. Here's my dependency property definition:
public static readonly DependencyProperty SelectAllProperty =
DependencyProperty.Register("SelectAll", typeof(bool), typeof(MultiSelectComboBox),
new PropertyMetadata(false));
And here's the test XAML I used:
<StackPanel
Orientation="Vertical"
>
<CheckBox Content="SelectAll" x:Name="SelectAllCheckBox" />
<local:MultiSelectComboBox
SelectAll="{Binding IsChecked, ElementName=SelectAllCheckBox}" />
</StackPanel>

Related

WPF MenuItem display without surrounding empty area

In the following XAML I need to display the blue rectangle inside menu item with no grey empty area surrounding it. But as shown in the screen below, the rectangle inside menu item is showing lots of empty space surrounding the rectangle. Question: What I may be doing wrong here and how we can get rid of the empty space from the menu item? I've tried the using Margin and StackPanel. But no success so far. I think we probably need some sort of menu item template here. I'm following this WPF tutorial.
XAML:
<Window x:Class="WpfTESTApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTESTApp"
mc:Ignorable="d"
Title="MainWindow" Height="492.182" Width="499" Loaded="Window_Loaded">
<Grid>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="My Rectangle">
<StackPanel Width="50" Height="50">
<Rectangle Fill="Blue" Width="50" Height="50" Margin="0,0,0,0" Stroke="Black"/>
</StackPanel>
</MenuItem>
</Menu>
</DockPanel>
</Grid>
</Window>
Screenshot of Main Window [with design and run-time view]: In the menu item I need to display the blue rectangle only
I believe what you need to do is to override the default control template of Menu Item . So i have taken the default control template and modified it remove the icon area from the template and applied that on the control
<Window.Resources>
<ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
<Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<Grid VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="0"/>
<Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Visibility="Collapsed" VerticalAlignment="Center"/>
<ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom">
<Border x:Name="SubMenuBorder" BorderBrush="#FF999999" BorderThickness="1" Padding="0" Background="#FFF0F0F0" Width="Auto" >
<ScrollViewer x:Name="SubMenuScrollViewer" Padding="0" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
</Canvas>
<Rectangle Fill="#FFD7D7D7" HorizontalAlignment="Left" Width="1"/>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
</Grid>
</ScrollViewer>
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSuspendingPopupAnimation" Value="True">
<Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
</Trigger>
<Trigger Property="Icon" Value="{x:Null}">
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>
<Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
<Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>
</Trigger>
<Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
<Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
<Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
And the code for the menu item will be as given below
<DockPanel>
<Menu DockPanel.Dock="Top" >
<MenuItem Header="My Rectangle" Template="{DynamicResource MenuItemControlTemplate1}" >
<Rectangle Fill="Blue" Width="200" Height="30" Margin="-35,0,-50,0" Stroke="Black"/>
<Rectangle Fill="Red" Width="200" Height="30" Margin="-35,0,-50,0" Stroke="Black"/>
</MenuItem>
</Menu>
</DockPanel>
As you can see i have applied the margin for the rectangle, it you remove that then you will be able to see padding at the left and right side.

WPF ListView IsSelected Set Border Opacity

I have a listview and padding must be 2 and color's getting system theme (using windows 8.1) but look very ugly.
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="True">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{x:Static SystemParameters.WindowGlassBrush}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="IsSelected" Value="true"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
TIP: I used CornerRadius = 10 in DataTemplate.
<DataTemplate x:Key="DT">
<StackPanel Orientation="Vertical">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<Border Background="{Binding HEXRengi}" MinHeight="100" MinWidth="150" Height="100" Width="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="2" CornerRadius="10"/>
<ToggleButton x:Name="HEXRenkAl" Style="{DynamicResource DüzRenkPaletiDüğmeBiçimi}" Grid.Row="0" Click="HEXRenkAl_Click">
<Border Background="{Binding HEXRengi}" MinHeight="50" MinWidth="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="10">
<TextBox Text="{Binding HEXRengi}" Style="{DynamicResource SeçilebilirAmaYazılamazMetinKutusu}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Justify" TextWrapping="Wrap" Foreground="White" FontFamily="Century Gothic" FontSize="16" FontWeight="Bold"/>
</Border>
</ToggleButton>
<ToggleButton x:Name="RGBRengiAl" Style="{DynamicResource DüzRenkPaletiDüğmeBiçimi}" Grid.Row="1" Click="RGBRengiAl_Click">
<Border Background="{Binding HEXRengi}" MinHeight="50" MinWidth="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="05">
<TextBox Text="{Binding RGBRengi}" Style="{DynamicResource SeçilebilirAmaYazılamazMetinKutusu}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Justify" TextWrapping="Wrap" Foreground="White" FontFamily="Century Gothic" FontSize="16" FontWeight="Bold"/>
</Border>
</ToggleButton>
</Grid>
</StackPanel>
</DataTemplate>
Look like this;
And IsSelected;
When i used setter property opacity value apply all item. Just should apply on border, not all item.
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{x:Static SystemParameters.WindowGlassBrush}"/>
<Setter TargetName="Border" Property="Opacity" Value=".5"/>
</Trigger>
How can i do it? Thanks for advices.
When i used setter property opacity value apply all item. Just should apply on border, not all item.
If you are wondering why opacity value is applying to all items, that is because you have named "Border" and you are targeting by name in your Trigger.
<Border Name="Border" Padding="2" SnapsToDevicePixels="True">
<ContentPresenter />
</Border>

How to have buttons with the same width inside a ListView/WrapPanel?

I have a ListView that shows a list of buttons inside a WrapPanel. The text inside the buttons may vary so the width of the buttons may also vary.
I'd like to have all the buttons to have same width, meaning I want all the buttons to have the width of the larger button inside the WrapPanel. Is there a way to do that?
Here's my ListView:
<ListView Margin="8 10 8 8"
VerticalAlignment="Top"
ScrollViewer.CanContentScroll="True"
Background="Transparent"
x:Name="ResourcesList"
BorderThickness="0"
ItemContainerStyle="{DynamicResource LvItemStyle}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding CadResource.Values}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<ItemContainerTemplate>
<Button Content="{Binding ResourceAlias.Value}"
x:Name="ResourceButton"
Command="{Binding ElementName=ResourcesList, Path=DataContext.SelectResourceCommand}"
CommandParameter="{Binding}"
VerticalContentAlignment="Stretch"
BorderThickness="0"
Margin="2"
MinWidth="75"
Template="{DynamicResource ResourceButtonTemplate}"
Foreground="{DynamicResource ResouceAliasText}"
Background="{Binding ResourceAlias.BackColor, Mode=TwoWay, Converter={converter:StringFormatConverter}}">
<Button.ContextMenu>
<ContextMenu Name="ContextMenu"
ItemsSource="{Binding ContextMenu}"
ItemContainerStyle="{StaticResource MenuItemStyle}" />
</Button.ContextMenu>
</Button>
</ItemContainerTemplate>
</ListView.ItemTemplate>
</ListView>
And here's the template for the button that I'm using:
<ControlTemplate x:Key="ResourceButtonTemplate"
TargetType="{x:Type ButtonBase}">
<Border x:Name="border"
Background="{DynamicResource ResourceButtonBackground}"
BorderThickness="1"
Height="20"
BorderBrush="{DynamicResource ResourceButtonBorder}"
SnapsToDevicePixels="True">
<Border BorderThickness="0 1 0 0"
Background="Transparent"
BorderBrush="{DynamicResource ResourceHighlight}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<images:ResourceSquare Fill="{TemplateBinding Background}"
Grid.Column="0"
Style="{StaticResource ViewBoxStyleChildWindow13X13}"
x:Name="ResourceSquare"
HorizontalAlignment="Left"
Margin="3 2 0 2" />
<images:ResourceTriangle Fill="{TemplateBinding Background}"
Grid.Column="0"
Style="{StaticResource ViewBoxStyleChildWindow13X13}"
x:Name="ResourceTriangle"
Visibility="Collapsed"
HorizontalAlignment="Left"
Margin="3 2 0 2" />
<ContentPresenter x:Name="contentPresenter"
Grid.Column="1"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
Focusable="False"
TextBlock.FontSize="{Binding FontSize}"
Margin="7 0 7 0"
RecognizesAccessKey="True"
HorizontalAlignment="Center"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
<images:ResourceConnected Fill="{DynamicResource IconToolbar}"
Grid.Column="2"
Style="{StaticResource ViewBoxStyleChildWindow10X10}"
HorizontalAlignment="Right"
VerticalAlignment="Top" />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected.Value}"
Value="True">
<Setter TargetName="border"
Property="Background"
Value="{DynamicResource ListSelectedLine}" />
<Setter TargetName="border"
Property="BorderBrush"
Value="{DynamicResource BorderHighLight}" />
</DataTrigger>
<Trigger Property="Button.IsDefaulted"
Value="True">
<Setter Property="BorderBrush"
TargetName="border"
Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
TargetName="border"
Value="{DynamicResource CadMouseOver}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
So here's what I did:
I added an event handler for SizeChanged on the WrapPanel
<WrapPanel HorizontalAlignment="Left" SizeChanged="WrapPanelSizeChanged" />
And the WrapPanelSizeChanged is as follows:
private void WrapPanelSizeChanged(object sender, EventArgs e)
{
var panel = sender as WrapPanel;
if(panel == null)
return;
if( panel.Children.Count == 0 )
return;
var itemWidth = panel.Children.OfType<FrameworkElement>().Max(x => x.ActualWidth);
panel.ItemWidth = itemWidth;
}
Well, the control you need is called WrapGrid. It is present in Windows Store apps, but not in WPF. Somebody calls it UniformWrapPanel. See this please:
Is there a WPF "WrapGrid" control available or an easy way to create one?
Please note, that WrapPanel has ItemWidth property. If you set it, all child elements will have this width. It would be very easy, if you could set it to fixed value.
You can try following. Add items to wrappanel, then measure theirs sizes, select widest element and set it's ActualWidth to WrapPanel.ItemWidth property. Then all elements should have the same width

Custom WPF-WindowStyle: How to recognize click on custom help button?

I've got a custom Style for my WPF Windows. One part of it is an additional HelpButton next to the regular minimize, maximize and Close button. Assigning this style to my Windows works as expected, but how does my window notice that this button was clicked? The button is defined in the style so my window does not really know about it. Here's the code for the style:
<ResourceDictionary x:Class="Company.Common.Themes.CompanyWindowStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Company.Common.Themes;component/CompanyWindowBaseStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="CompanyWindowStyle"
BasedOn="{StaticResource CompanyWindowBaseStyle}"
TargetType="{x:Type Window}">
<Setter Property="ResizeMode"
Value="CanResize" />
<EventSetter Event="Loaded"
Handler="WindowLoaded" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border x:Name="PART_Container"
Padding="7,7,7,5">
<Grid TextOptions.TextFormattingMode="Display"
TextOptions.TextRenderingMode="ClearType">
<Border x:Name="PART_Border"
Width="Auto"
Height="Auto"
Background="{StaticResource BackgroundBrush}"
BorderBrush="{StaticResource BorderBrush}"
BorderThickness="1">
<DockPanel HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent">
<Border x:Name="TitleBar"
Background="{StaticResource BackgroundBrush}"
BorderThickness="0"
DockPanel.Dock="Top"
MouseDown="TitleBarMouseDown">
<Grid Height="32"
Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="36" />
<ColumnDefinition />
<ColumnDefinition Width="34" />
<ColumnDefinition Width="34" />
<ColumnDefinition Width="34" />
<ColumnDefinition Width="34" />
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="0"
MouseDoubleClick="IconMouseDoubleClicked">
<Image x:Name="Icon"
Width="32"
Height="32"
Margin="4,-7,0,7"
HorizontalAlignment="Right"
WindowChrome.IsHitTestVisibleInChrome="True"
Source="{Binding Path=Icon,
Mode=OneWay,
RelativeSource={RelativeSource TemplatedParent}}" />
</ContentControl>
<TextBlock x:Name="Caption"
Grid.Column="1"
Margin="4,0,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Segoe UI"
FontSize="12"
Opacity="0.66"
Text="{Binding Path=Title,
Mode=OneWay,
RelativeSource={RelativeSource TemplatedParent}}" />
<Button x:Name="HelpButton"
Grid.Column="2"
Width="34"
Height="26"
VerticalAlignment="Top"
Click="HelpButtonClick"
Style="{StaticResource TitleBarButtonStyle}"
WindowChrome.IsHitTestVisibleInChrome="True">
<Grid MaxWidth="9"
MaxHeight="9">
<TextBlock Text="?" FontSize="14"
Foreground="{Binding RelativeSource={RelativeSource AncestorType=Button},
Path=Foreground}" />
</Grid>
</Button>
<Button x:Name="MinButton"
Grid.Column="3"
Width="34"
Height="26"
VerticalAlignment="Top"
Click="MinButtonClick"
Style="{StaticResource TitleBarButtonStyle}"
WindowChrome.IsHitTestVisibleInChrome="True">
<Grid MaxWidth="9"
MaxHeight="9">
<Path Data="M0,8 H8 M0,7 H8 M0,6 H8"
RenderOptions.EdgeMode="Aliased"
Stretch="None"
Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button},
Path=Foreground}"
StrokeThickness="1" />
</Grid>
</Button>
<Button x:Name="MaxButton"
Grid.Column="4"
Width="34"
Height="26"
VerticalAlignment="Top"
Click="MaxButtonClick"
Style="{StaticResource TitleBarButtonStyle}"
WindowChrome.IsHitTestVisibleInChrome="True">
<Grid MaxWidth="9"
MaxHeight="9">
<Path x:Name="PART_MaxButton_Path"
Data="M0,0 H8 V8 H0 V0 M0,1 H8 M0,2 H8"
RenderOptions.EdgeMode="Aliased"
Stretch="None"
Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button},
Path=Foreground}"
StrokeThickness="1" />
</Grid>
</Button>
<Button x:Name="CloseButton"
Grid.Column="5"
Width="34"
Height="26"
VerticalAlignment="Top"
Click="CloseButtonClick"
Style="{StaticResource TitleBarButtonStyle}"
WindowChrome.IsHitTestVisibleInChrome="True">
<Grid MaxWidth="9"
MaxHeight="9">
<Path Data="M0,0 L8,8 M0,8 L8,0"
Stretch="None"
Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button},
Path=Foreground}"
StrokeThickness="1.5" />
</Grid>
</Button>
</Grid>
</Border>
<ContentPresenter />
</DockPanel>
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="WindowState"
Value="Maximized">
<Setter TargetName="PART_MaxButton_Path"
Property="Data"
Value="M0,3 H5 V8 H0 V3 M0,4 H5 M3,0 H8 V5 H7 M3,1 H8" />
</Trigger>
<Trigger Property="WindowState"
Value="Normal">
<Setter TargetName="PART_Border"
Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="7"
Direction="315"
Opacity="0.5"
ShadowDepth="2"
Color="black" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsActive"
Value="False">
<Setter TargetName="PART_Border"
Property="BorderBrush"
Value="{StaticResource BorderBrushInactive}" />
</Trigger>
<Trigger Property="IsActive"
Value="True">
<Setter TargetName="PART_Border"
Property="BorderBrush"
Value="{StaticResource BorderBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
I have somehow no idea how it is done. Can someone please point me in the right direction?
UPDATE:
This is the header of my MainWindow's XAML where I use the Style:
<Window x:Class="Company.MainGUI.GUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="clr-namespace:Company.Common;assembly=Company.Common"
xmlns:controls="clr-namespace:Company.Common.Controls;assembly=Company.Common.Controls"
xmlns:l="clr-namespace:Company.Common.Languages;assembly=Company.Common.Languages"
xmlns:local="clr-namespace:Company.MainGUI.GUI"
Title="{x:Static l:Common_Text.Application_Name}"
UseLayoutRounding="True"
MinWidth="900"
MinHeight="600"
Icon="pack://application:,,,/Company.Common.Images;component/Resources/ApplicationLogo_MainGUI.ico"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Company.Common.Themes;component/CompanyWindowStyle.xaml" />
<ResourceDictionary Source="pack://application:,,,/Company.Common.Themes;component/MainGUISkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Window.Style>
<Style TargetType="{x:Type local:MainWindow}"
BasedOn="{StaticResource CompanyWindowStyle}" />
</Window.Style>

WPF ComboBox Residing in a ListBox Click Event

I have a little issue.
I have a a ListBox that is bound to a list of object. These Objects have a property that can be set. I have in the LIstBox ItemTemplate (a datatemplate) a combobox that is about to the objects property and the combobox has some hardcoded values.
My problem is that when the list box is displayed and I click ont he combo, only the ListBoxItem is selected, The click never makes it to the combobox!
just to give you an idea.
<ListBox SelectionMode="Multiple" VerticalAlignment="Center" HorizontalAlignment="Left" Style="{StaticResource Style_ListBox}" ItemsSource="{Binding ModeSampleSets, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate DataType="ListBoxItem">
<Grid>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" FontWeight="Bold" Text="{Binding Name, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/>
<ComboBox Focusable="False" Width="10" Height="10" Grid.Column="2" Style="{StaticResource Style_ComboBoxColorPicker}" SelectedItem="{Binding GraphColor, Mode=TwoWay}" >
<ComboBoxItem IsSelected="True" Content="#e62a2c" />
<ComboBoxItem Content="#ec7c28"></ComboBoxItem>
<ComboBoxItem Content="#69c5d8"></ComboBoxItem>
<ComboBoxItem Content="#36b34b"></ComboBoxItem>
<ComboBoxItem Content="#415dae"></ComboBoxItem>
<ComboBoxItem Content="#9056A3"></ComboBoxItem>
<ComboBoxItem Content="#0b0b0b"></ComboBoxItem>
<ComboBoxItem Content="#666666"></ComboBoxItem>
<ComboBoxItem Content="#a6a6a6"></ComboBoxItem>
</ComboBox>
</Grid>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="8" Text="{Binding SampleAnalysisDate, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4,0,0">
<TextBlock FontSize="14" Text="{Binding WaveLengthStart, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/>
<TextBlock Margin="2,0,0,0" FontSize="14" Text="{x:Static UIStrings:WaveLengthScanStrings.WaveLengthScanModeView_SampleWaveLength_Seperator}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/>
<TextBlock Margin="2,0,0,0" FontSize="14" Text="{Binding WaveLengthStop, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Styles:
<Style x:Key="Style_ComboBoxColorPickerItemContainerStyle" TargetType="ComboBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border Name="ItemBorder" Margin="2" BorderBrush="Transparent" BorderThickness="1.5" Background="Transparent">
<ContentPresenter Margin="2" Height="20" Width="20" ContentTemplate="{StaticResource DataTemplate_ComboBoxColorPickerItemTemplate}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="ItemBorder" Property="BorderBrush" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="Style_ComboBoxColorPicker" TargetType="ComboBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel MaxWidth="100"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate" Value="{StaticResource DataTemplate_ComboBoxColorPickerItemTemplate}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource Style_ComboBoxColorPickerItemContainerStyle}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton
ClickMode="Press"
Name="ComboToggleButton"
IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
Focusable="False"
>
<ToggleButton.Template>
<ControlTemplate TargetType="ToggleButton">
<ContentPresenter/>
</ControlTemplate>
</ToggleButton.Template>
<ContentPresenter Content="{TemplateBinding ComboBox.SelectionBoxItem}" ContentTemplate="{TemplateBinding ComboBox.ItemTemplate}" ContentTemplateSelector="{TemplateBinding ComboBox.ItemTemplateSelector}"/>
</ToggleButton>
<Popup
Placement="Bottom"
Name="Popup"
Focusable="False"
AllowsTransparency="True"
IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
PopupAnimation="Fade">
<Grid
MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"
MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}"
Name="DropDown"
SnapsToDevicePixels="True">
<Border
BorderBrush="Gray"
BorderThickness="1.5"
Name="DropDownBorder"
Background="White">
<ScrollViewer
Margin="0"
SnapsToDevicePixels="True">
<ItemsPresenter />
</ScrollViewer>
</Border>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="Style_ListBox" TargetType="ListBox">
<Setter Property="BorderBrush" Value="{StaticResource Brush_PanelInnerBorder}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Background="{StaticResource Brush_PanelInnerBackground}"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Background="{StaticResource Brush_PanelInnerBackground}" Margin="-.5">
<Border x:Name="BorderItem" Margin="0" ClipToBounds="True" BorderThickness="0" Style="{StaticResource Style_PanelInnerBorder}">
<Rectangle x:Name="BackgroundRec" Fill="Transparent" Stroke="Transparent" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Border>
<ContentPresenter Name="TheContentPresenter" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Border Margin="0" ClipToBounds="True" Style="{StaticResource Style_PanelInnerBorder}">
<Rectangle VerticalAlignment="Bottom" Width="{TemplateBinding Width}" Height="0"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="BorderItem" Property="Background" Value="{StaticResource Brush_Highlight}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
Make sure you don't have IsHitTestVisible="False" on the ComboBox's style. Can you please post here the styles you're using?
I found the problem.
My ItemContainerStyle Template had a rectangle and i guess it was implying zindex somehow. When I expclicity told it to draw the content before the border and it's rectangle, it worked. i just had to add Panel.ZIndex="1" and it worked.

Resources