title: Creating custom combo box in WPF
body: <p>I would like to create a custom WPF control that inherits from comboBox. So far, I created my project and changed the inheritance from Control to ComboBox since this is what I want. I tried to update the visual by going into Generic.xaml and change the template from the default one to a copy of a normal combo box (I used Blend to create a copy of the template and copied it). This is where it starts to crash.</p>
  
  <p>For now I get the error
      Cannot find the static member 'MaxDropDownHeightProperty' on the type 'Control'.</p>
  
  <p>I do not get any grooved line showing where the error is but I have an idea where it is. </p>
  
  <p>I think I understand that this mean that the template still thinks it's a template of 'Control' type, but how do I change that into ComboBox.</p>
  
  <p>Regards, </p>
  
  <p>Edit : I am fully aware that copying the template of the combox box isn't really usefull if I am going to use exactly that same, but that fact is that I would like change it evenually, hence the need to.</p>
  
  <p>Here is the General.xaml and my code page. It's pretty long</p>
  
  <p>SearchComboBox.vb :</p>
  
  <pre><code>Public Class SearchComboBox
  Inherits ComboBox
  
  Public Shared ReadOnly OriginalItemSourceProperty As DependencyProperty
  
  Private _OriginalItemSource As System.Collections.IEnumerable
  
  Shared Sub New()
      'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
      'This style is defined in Themes\Generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(GetType(SearchComboBox), New FrameworkPropertyMetadata(GetType(SearchComboBox)))
  
      Dim md As New FrameworkPropertyMetadata()
      SearchComboBox.OriginalItemSourceProperty = DependencyProperty.Register("OriginalSource", GetType(System.Collections.IEnumerable), GetType(SearchComboBox), md)
  
  End Sub
  
  Public Sub New()
  
      MyBase.IsEditable = True
  
  
  End Sub
  
  Public Property OriginalSource As System.Collections.IEnumerable
      Get
          Return CType(GetValue(SearchComboBox.OriginalItemSourceProperty), System.Collections.IEnumerable)
      End Get
      Set(ByVal value As System.Collections.IEnumerable)
          SetValue(SearchComboBox.OriginalItemSourceProperty, value)
          MyBase.ItemsSource = value
      End Set
  End Property
  
  End Class
  </code></pre>
  
  <p>Generic.xaml :</p>
  
  <pre><code>&lt;ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
  xmlns:local="clr-namespace:SeachComboBox"&gt;
  
    &lt;Style TargetType="{x:Type local:SearchComboBox}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate&gt;
                  &lt;Grid x:Name="Placement" SnapsToDevicePixels="True"&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="*"/&gt;
                          &lt;ColumnDefinition Width="Auto"/&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"&gt;
                          &lt;Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=Placement}"&gt;
                              &lt;Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"&gt;
                                  &lt;ScrollViewer x:Name="DropDownScrollViewer"&gt;
                                      &lt;Grid RenderOptions.ClearTypeHint="Enabled"&gt;
                                          &lt;Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"&gt;
                                              &lt;Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/&gt;
                                          &lt;/Canvas&gt;
                                          &lt;ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
                                      &lt;/Grid&gt;
                                  &lt;/ScrollViewer&gt;
                              &lt;/Border&gt;
                          &lt;/Microsoft_Windows_Themes:SystemDropShadowChrome&gt;
                      &lt;/Popup&gt;
                      &lt;Microsoft_Windows_Themes:ListBoxChrome x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"/&gt;
                      &lt;TextBox x:Name="PART_EditableTextBox" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"&gt;
                          &lt;TextBox.Style&gt;
                              &lt;Style TargetType="{x:Type TextBox}"&gt;
                                  &lt;Setter Property="OverridesDefaultStyle" Value="True"/&gt;
                                  &lt;Setter Property="AllowDrop" Value="True"/&gt;
                                  &lt;Setter Property="MinWidth" Value="0"/&gt;
                                  &lt;Setter Property="MinHeight" Value="0"/&gt;
                                  &lt;Setter Property="FocusVisualStyle" Value="{x:Null}"/&gt;
                                  &lt;Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/&gt;
                                  &lt;Setter Property="Stylus.IsFlicksEnabled" Value="False"/&gt;
                                  &lt;Setter Property="Template"&gt;
                                      &lt;Setter.Value&gt;
                                          &lt;ControlTemplate TargetType="{x:Type TextBox}"&gt;
                                              &lt;ScrollViewer x:Name="PART_ContentHost" Background="{TemplateBinding Background}" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/&gt;
                                              &lt;ControlTemplate.Triggers&gt;
                                                  &lt;Trigger Property="IsEnabled" Value="False"&gt;
                                                      &lt;Setter Property="FontWeight" Value="Bold"/&gt;
                                                      &lt;Setter Property="Background" Value="#FFEDDDDD"/&gt;
                                                  &lt;/Trigger&gt;
                                              &lt;/ControlTemplate.Triggers&gt;
                                          &lt;/ControlTemplate&gt;
                                      &lt;/Setter.Value&gt;
                                  &lt;/Setter&gt;
                              &lt;/Style&gt;
                          &lt;/TextBox.Style&gt;
                      &lt;/TextBox&gt;
                      &lt;ToggleButton Grid.Column="1" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"&gt;
                          &lt;ToggleButton.Style&gt;
                              &lt;Style TargetType="{x:Type ToggleButton}"&gt;
                                  &lt;Setter Property="OverridesDefaultStyle" Value="True"/&gt;
                                  &lt;Setter Property="IsTabStop" Value="False"/&gt;
                                  &lt;Setter Property="Focusable" Value="False"/&gt;
                                  &lt;Setter Property="ClickMode" Value="Press"/&gt;
                                  &lt;Setter Property="Template"&gt;
                                      &lt;Setter.Value&gt;
                                          &lt;ControlTemplate TargetType="{x:Type ToggleButton}"&gt;
                                              &lt;Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RoundCorners="False" SnapsToDevicePixels="True" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"&gt;
                                                  &lt;Path x:Name="Arrow" Data="M0,0L3.5,4 7,0z" Fill="Black" HorizontalAlignment="Center" Margin="0,1,0,0" VerticalAlignment="Center"/&gt;
                                              &lt;/Microsoft_Windows_Themes:ButtonChrome&gt;
                                              &lt;ControlTemplate.Triggers&gt;
                                                  &lt;Trigger Property="IsChecked" Value="True"&gt;
                                                      &lt;Setter Property="RenderPressed" TargetName="Chrome" Value="True"/&gt;
                                                  &lt;/Trigger&gt;
                                                  &lt;Trigger Property="IsEnabled" Value="False"&gt;
                                                      &lt;Setter Property="Fill" TargetName="Arrow" Value="#FFEDDDDD"/&gt;
                                                  &lt;/Trigger&gt;
                                              &lt;/ControlTemplate.Triggers&gt;
                                          &lt;/ControlTemplate&gt;
                                      &lt;/Setter.Value&gt;
                                  &lt;/Setter&gt;
                              &lt;/Style&gt;
                          &lt;/ToggleButton.Style&gt;
                      &lt;/ToggleButton&gt;
                  &lt;/Grid&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsKeyboardFocusWithin" Value="True"&gt;
                          &lt;Setter Property="Foreground" Value="Black"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsDropDownOpen" Value="True"&gt;
                          &lt;Setter Property="RenderFocused" TargetName="Border" Value="True"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="HasItems" Value="False"&gt;
                          &lt;Setter Property="Height" TargetName="DropDownBorder" Value="95"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsEnabled" Value="False"&gt;
                          &lt;Setter Property="FontWeight" Value="Bold"/&gt;
                          &lt;Setter Property="Background" Value="#FFEDDDDD"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsGrouping" Value="True"&gt;
                          &lt;Setter Property="ScrollViewer.CanContentScroll" Value="False"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True"&gt;
                          &lt;Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/&gt;
                          &lt;Setter Property="Color" TargetName="Shdw" Value="#71000000"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="CanContentScroll" SourceName="DropDownScrollViewer" Value="False"&gt;
                          &lt;Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/&gt;
                          &lt;Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>