title: WPF - Implement ItemTemplate for an ItemsPresenter?
body: <p>I've been working on a SplitButton control for WPF and its basically done, but I'm trying to go through all the possible properties that can be set on it and make sure they are actually implemented. I mostly have only two properties left to implement which is the <code>ItemTemplate</code> and <code>ItemTemplateSelector</code> (and <code>AlternationCount</code>, okay so 3 properties).</p>
  
  <p>I was able to get the <code>HeaderTemplate</code> and <code>HeaderTemplateSelector</code> working by binding the <code>ContentTemplate</code> and <code>ContentTemplateSelector</code> to them on a <code>ContentPresenter</code>. This is for the button part of the control though. For the drop-drop part of the control I'm using a Popup, Border, and <code>ItemsPresenter</code>. The problem is that I can't figure how to set the <code>ItemTemplate</code> and <code>ItemTemplateSelector</code> properties for the <code>ItemsPresenter</code>.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p><strong>Update</strong>:
  The full source code for the SplitButton is now available at:
  <a href="http://anothersplitbutton.codeplex.com/" rel="nofollow noreferrer">http://anothersplitbutton.codeplex.com/</a></p>
  
  <p>Here's the Luna.NormalColor.xaml file:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:WpfSplitButton="clr-namespace:WpfSplitButton"
                      xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
      &lt;!-- SplitButtonHeader Style --&gt;
      &lt;Style x:Key="{x:Type WpfSplitButton:SplitButtonHeader}"
             TargetType="{x:Type WpfSplitButton:SplitButtonHeader}"&gt;
  
          &lt;Style.Resources&gt;
              &lt;Style x:Key="ButtonFocusVisual"&gt;
                  &lt;Setter Property="Control.Template"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate&gt;
                              &lt;Rectangle Margin="2"
                                         StrokeThickness="1"
                                         Stroke="Black"
                                         StrokeDashArray="1 2"
                                         SnapsToDevicePixels="True" /&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/Style.Resources&gt;
  
          &lt;Style.Setters&gt;
              &lt;Setter Property="FocusVisualStyle"
                      Value="{StaticResource ButtonFocusVisual}" /&gt;
              &lt;Setter Property="HorizontalContentAlignment"
                      Value="Center" /&gt;
              &lt;Setter Property="PastLeftDetection"
                      Value="True" /&gt;
              &lt;Setter Property="VerticalContentAlignment"
                      Value="Center" /&gt;
  
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate&gt;
                          &lt;mwt:ButtonChrome x:Name="Chrome"
                                            BorderBrush="{TemplateBinding Border.BorderBrush}"
                                            RenderDefaulted="{TemplateBinding Button.IsDefaulted}"
                                            RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}"
                                            RenderPressed="{TemplateBinding ButtonBase.IsPressed}"
                                            SnapsToDevicePixels="True"&gt;
  
                              &lt;Grid Background="{TemplateBinding ButtonBase.Background}"&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width="*" /&gt;
                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
  
                                  &lt;ContentPresenter Content="{TemplateBinding ContentControl.Content}"
                                                    ContentTemplate="{TemplateBinding ButtonBase.ContentTemplate}"
                                                    ContentTemplateSelector="{TemplateBinding ButtonBase.ContentTemplateSelector}"
                                                    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                                    Margin="{TemplateBinding Control.Padding}"
                                                    RecognizesAccessKey="True"
                                                    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                                    VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" /&gt;
  
                                  &lt;Border x:Name="PART_DropDownInitiator"
                                          Background="Transparent"
                                          BorderBrush="{Binding Path=BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfSplitButton:SplitButtonHeader}}}"
                                          BorderThickness="1,0,0,0"
                                          Grid.Column="1"
                                          HorizontalAlignment="Stretch"
                                          Margin="0,0,0,0"
                                          Padding="4,0,4,0"
                                          VerticalAlignment="Stretch"&gt;
  
                                      &lt;Path Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfSplitButton:SplitButtonHeader}}}"
                                            VerticalAlignment="Center"&gt;
  
                                          &lt;Path.Style&gt;
                                              &lt;Style&gt;
                                                  &lt;Setter Property="Path.Fill"
                                                          Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" /&gt;
  
                                                  &lt;Style.Triggers&gt;
                                                      &lt;Trigger Property="WpfSplitButton:SplitButton.IsMouseOver"
                                                               Value="True"&gt;
                                                          &lt;Setter Property="Path.Fill"
                                                                  Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /&gt;
                                                      &lt;/Trigger&gt;
                                                  &lt;/Style.Triggers&gt;
                                              &lt;/Style&gt;
                                          &lt;/Path.Style&gt;
  
                                          &lt;Path.Data&gt;
                                              &lt;PathGeometry&gt;
                                                  &lt;PathGeometry.Figures&gt;
                                                      &lt;PathFigureCollection&gt;
                                                          &lt;PathFigure IsClosed="True"
                                                                      StartPoint="0,0"&gt;
                                                              &lt;PathFigure.Segments&gt;
                                                                  &lt;PathSegmentCollection&gt;
                                                                      &lt;LineSegment Point="8,0" /&gt;
                                                                      &lt;LineSegment Point="4,5" /&gt;
                                                                  &lt;/PathSegmentCollection&gt;
                                                              &lt;/PathFigure.Segments&gt;
                                                          &lt;/PathFigure&gt;
                                                      &lt;/PathFigureCollection&gt;
                                                  &lt;/PathGeometry.Figures&gt;
                                              &lt;/PathGeometry&gt;
                                          &lt;/Path.Data&gt;
                                      &lt;/Path&gt;
                                  &lt;/Border&gt;
                              &lt;/Grid&gt;
                          &lt;/mwt:ButtonChrome&gt;
  
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="UIElement.IsKeyboardFocused"
                                       Value="True"&gt;
                                  &lt;Setter TargetName="Chrome"
                                          Property="mwt:ButtonChrome.RenderDefaulted"
                                          Value="True" /&gt;
                              &lt;/Trigger&gt;
  
                              &lt;Trigger Property="ToggleButton.IsChecked"
                                       Value="True"&gt;
                                  &lt;Setter TargetName="Chrome"
                                          Property="mwt:ButtonChrome.RenderPressed"
                                          Value="True" /&gt;
                              &lt;/Trigger&gt;
                          &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style.Setters&gt;
      &lt;/Style&gt;
  
      &lt;!-- SplitButton --&gt;
      &lt;Style x:Key="{x:Type WpfSplitButton:SplitButton}"
             TargetType="{x:Type WpfSplitButton:SplitButton}"&gt;
  
          &lt;Style.Resources&gt;
              &lt;LinearGradientBrush x:Key="ButtonNormalBackgroundFill"
                                   EndPoint="0.5,1"
                                   StartPoint="0.5,0"&gt;
                  &lt;LinearGradientBrush.GradientStops&gt;
                      &lt;GradientStop Color="#FFFFFFFF"
                                    Offset="0" /&gt;
                      &lt;GradientStop Color="#FFF0F0EA"
                                    Offset="0.9" /&gt;
                  &lt;/LinearGradientBrush.GradientStops&gt;
              &lt;/LinearGradientBrush&gt;
  
              &lt;SolidColorBrush x:Key="ButtonBorder"
                               Color="#FF003C74" /&gt;
          &lt;/Style.Resources&gt;
  
          &lt;Setter Property="AutoUpdateHeader"
                  Value="True" /&gt;
  
          &lt;Setter Property="Background"
                  Value="{StaticResource ButtonNormalBackgroundFill}" /&gt;
  
          &lt;Setter Property="BorderBrush"
                  Value="{StaticResource ButtonBorder}" /&gt;
  
          &lt;Setter Property="BorderThickness"
                  Value="1" /&gt;
  
          &lt;Setter Property="Foreground"
                  Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /&gt;
  
          &lt;Setter Property="HorizontalContentAlignment"
                  Value="Center" /&gt;
  
          &lt;Setter Property="Padding"
                  Value="4,4,4,4" /&gt;
  
          &lt;Setter Property="VerticalContentAlignment"
                  Value="Center" /&gt;
  
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type WpfSplitButton:SplitButton}"&gt;
                      &lt;Grid&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="*" /&gt;
                              &lt;RowDefinition Height="Auto" /&gt;
                          &lt;/Grid.RowDefinitions&gt;
  
                          &lt;WpfSplitButton:SplitButtonHeader x:Name="PART_Header"
                                                            Background="{TemplateBinding Background}"
                                                            BorderBrush="{TemplateBinding BorderBrush}"
                                                            BorderThickness="{TemplateBinding BorderThickness}"
                                                            Content="{TemplateBinding Header}"
                                                            ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                            ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                                            Cursor="{TemplateBinding Cursor}"
                                                            Foreground="{TemplateBinding Foreground}"
                                                            Grid.Row="0"
                                                            HorizontalAlignment="Stretch"
                                                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                            Padding="{TemplateBinding Padding}"
                                                            VerticalAlignment="Stretch"
                                                            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" /&gt;
  
                          &lt;Popup x:Name="PART_Popup"
                                 AllowsTransparency="True"
                                 Grid.Row="1"
                                 IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                 MinWidth="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfSplitButton:SplitButton}}}"
                                 Placement="Bottom"
                                 PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfSplitButton:SplitButton}}}"
                                 PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                                 StaysOpen="False"&gt;
  
                              &lt;mwt:SystemDropShadowChrome Color="Transparent"&gt;
                                  &lt;Border x:Name="DropDownBorder"
                                          Background="{TemplateBinding Background}"
                                          BorderBrush="{TemplateBinding BorderBrush}"
                                          BorderThickness="{TemplateBinding BorderThickness}"&gt;
  
                                      &lt;ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /&gt;
                                  &lt;/Border&gt;
                              &lt;/mwt:SystemDropShadowChrome&gt;
                          &lt;/Popup&gt;
                      &lt;/Grid&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
  
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="IsEnabled"
                       Value="False"&gt;
                  &lt;Setter Property="Foreground"
                          Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Update 2</strong>
  I tried switching out the ItemsPresenter with a ItemsControl, but I can't seem to get the ItemTemplate property to do anything. Here's the changed section of code:</p>
  
  <pre><code>&lt;ItemsControl DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfSplitButton:SplitButton}}}"
                AlternationCount="{TemplateBinding AlternationCount}"
                IsTabStop="{TemplateBinding IsTabStop}"
                IsTextSearchEnabled="{TemplateBinding IsTextSearchEnabled}"
                ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                ItemContainerStyleSelector="{TemplateBinding ItemContainerStyleSelector}"
                ItemBindingGroup="{TemplateBinding ItemBindingGroup}"
                ItemsPanel="{TemplateBinding ItemsPanel}"
                ItemsSource="{Binding Path=Items}"
                ItemStringFormat="{TemplateBinding ItemStringFormat}"
                ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBlock Text="HELLO" /&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  </code></pre>