Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Creating a ControlTemplate for a ComboBox

$
0
0

Hi everybody,

I'm trying to create a control template for a WPF ComboBox. Here is my 2 files :

<Window x:Class="MyProject.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><ResourceDictionary Source="/MyProject;component/resources/ResourceAssetTypes.xaml"/></Window.Resources><Grid><ComboBox Template="{DynamicResource ResourceKey=TemplateComboBox}"/></Grid></Window>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" xmlns:System="clr-namespace:System;assembly=mscorlib"><ControlTemplate x:Key="TemplateComboBox" TargetType="ComboBox"><Grid x:Name="Placement" SnapsToDevicePixels="True"><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"><Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=Placement}"><Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"><ScrollViewer x:Name="DropDownScrollViewer"><Grid RenderOptions.ClearTypeHint="Enabled"><Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"><Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/></Canvas><ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/></Grid></ScrollViewer></Border></Themes:SystemDropShadowChrome></Popup><Themes:ListBoxChrome x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"/><TextBox x:Name="PART_EditableTextBox" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"><TextBox.Style><Style TargetType="{x:Type TextBox}"><Setter Property="OverridesDefaultStyle" Value="True"/><Setter Property="AllowDrop" Value="True"/><Setter Property="MinWidth" Value="0"/><Setter Property="MinHeight" Value="0"/><Setter Property="FocusVisualStyle" Value="{x:Null}"/><Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/><Setter Property="Stylus.IsFlicksEnabled" Value="False"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type TextBox}"><ScrollViewer x:Name="PART_ContentHost" Background="Transparent" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/></ControlTemplate></Setter.Value></Setter></Style></TextBox.Style></TextBox><ToggleButton Grid.Column="1" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"><ToggleButton.Style><Style TargetType="{x:Type ToggleButton}"><Setter Property="OverridesDefaultStyle" Value="True"/><Setter Property="IsTabStop" Value="False"/><Setter Property="Focusable" Value="False"/><Setter Property="ClickMode" Value="Press"/><Setter Property="Background" Value="Transparent"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ToggleButton}"><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}}"><Path x:Name="Arrow" Data="M0,0L3.5,4 7,0z" Fill="Black" HorizontalAlignment="Center" Margin="0,1,0,0" VerticalAlignment="Center"/></Themes:ButtonChrome><ControlTemplate.Triggers><Trigger Property="IsChecked" Value="True"><Setter Property="RenderPressed" TargetName="Chrome" Value="True"/></Trigger><Trigger Property="IsEnabled" Value="False"><Setter Property="Fill" TargetName="Arrow" Value="#FFAFAFAF"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></ToggleButton.Style></ToggleButton></Grid><ControlTemplate.Triggers><Trigger Property="IsKeyboardFocusWithin" Value="True"><Setter Property="Foreground" Value="Black"/></Trigger><Trigger Property="IsDropDownOpen" Value="True"><Setter Property="RenderFocused" TargetName="Border" Value="True"/></Trigger><Trigger Property="HasItems" Value="False"><Setter Property="Height" TargetName="DropDownBorder" Value="95"/></Trigger><Trigger Property="IsEnabled" Value="False"><Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/><Setter Property="Background" Value="#FFF4F4F4"/></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsGrouping" Value="True"/><Condition><Condition.Value><System:Boolean>False</System:Boolean></Condition.Value></Condition></MultiTrigger.Conditions><Setter Property="ScrollViewer.CanContentScroll" Value="False"/></MultiTrigger><Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True"><Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/><Setter Property="Color" TargetName="Shdw" Value="#71000000"/></Trigger><Trigger Property="CanContentScroll" SourceName="DropDownScrollViewer" Value="False"><Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/><Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></ResourceDictionary>

But if I launch the application, I have an exception saying "Must have a non-null value for property".

Do you know how I can make my application work with a template for the ComboBox ?

Thank in advance.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>