123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <local:OwnerDrawComboBox x:Class="CustomControls.ComboBox"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:CustomControls">
- <ComboBox.Resources>
- <SolidColorBrush x:Key="ComboBox.Static.Background" Color="White"/>
- <SolidColorBrush x:Key="ComboBox.Static.Border" Color="#FFACACAC"/>
- <SolidColorBrush x:Key="ComboBox.Static.Glyph" Color="#FF606060"/>
- <LinearGradientBrush x:Key="ComboBox.MouseOver.Background" EndPoint="0,1" StartPoint="0,0">
- <GradientStop Color="#FFECF4FC" Offset="0.0"/>
- <GradientStop Color="#FFDCECFC" Offset="1.0"/>
- </LinearGradientBrush>
- <SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="#FF7EB4EA"/>
- <SolidColorBrush x:Key="ComboBox.MouseOver.Glyph" Color="#FF000000"/>
- <SolidColorBrush x:Key="ComboBox.Disabled.Background" Color="#FFF0F0F0"/>
- <SolidColorBrush x:Key="ComboBox.Disabled.Border" Color="#FFD9D9D9"/>
- <SolidColorBrush x:Key="ComboBox.Disabled.Glyph" Color="#FFBFBFBF"/>
- <Style x:Key="ComboBoxToggleButton" 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="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ToggleButton}">
- <Border x:Name="templateRoot" Background="{StaticResource ComboBox.Static.Background}" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
- <Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="19">
- <Path x:Name="arrow" Data="F1 M 0,0 L 4,4 L 8,0 " Stroke="{StaticResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
- </Border>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsMouseOver" Value="true">
- <Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Background}"/>
- <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Border}"/>
- </Trigger>
- <Trigger Property="IsEnabled" Value="false">
- <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource ComboBox.Disabled.Glyph}"/>
- <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Background}"/>
- <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Border}"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
- <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="*"/>
- <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
- </Grid.ColumnDefinitions>
- <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
- <Border x:Name="dropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
- <ScrollViewer x:Name="DropDownScrollViewer">
- <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
- </ScrollViewer>
- </Border>
- </Popup>
- <ToggleButton x:Name="toggleButton" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
- <ContentPresenter x:Name="contentPresenter" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" HorizontalAlignment="Stretch" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
- <local:OwnerDrawComboBoxItem x:Name="nullItem" Visibility="Hidden" IsHitTestVisible="False" Margin="{TemplateBinding Padding}"/>
- </Grid>
- <ControlTemplate.Triggers>
- <Trigger Property="SelectedIndex" Value="-1">
- <Setter Property="Visibility" TargetName="nullItem" Value="Visible"/>
- <Setter Property="Visibility" TargetName="contentPresenter" Value="Collapsed"/>
- <Setter Property="IsSelectedChanged" TargetName="nullItem" Value="true"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- <SolidColorBrush x:Key="TextBox.Static.Background" Color="#FFFFFFFF"/>
- <Style x:Key="ComboBoxEditableTextBox" 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>
- <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
- <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="*"/>
- <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
- </Grid.ColumnDefinitions>
- <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
- <Border x:Name="dropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
- <ScrollViewer x:Name="DropDownScrollViewer">
- <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
- <Canvas x:Name="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>
- </Popup>
- <ToggleButton x:Name="toggleButton" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
- <TextBox x:Name="textBox" Text="{Binding ComboText, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}" TextChanged="TextBox_TextChanged" PreviewMouseLeftButtonDown="TextBox_PreviewMouseLeftButtonDown" GotKeyboardFocus="TextBox_GotKeyboardFocus" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
- </Grid>
- <ControlTemplate.Triggers>
- <Trigger Property="IsEnabled" Value="false">
- <Setter Property="Opacity" TargetName="textBox" Value="0.5"/>
- </Trigger>
- <Trigger Property="HasItems" Value="false">
- <Setter Property="Height" TargetName="dropDownBorder" Value="95"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- <Style x:Key="ItemContainerStyle1" TargetType="{x:Type ComboBoxItem}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ComboBoxItem}">
- <local:OwnerDrawComboBoxItem x:Name="item" SnapsToDevicePixels="true"/>
- <ControlTemplate.Triggers>
- <Trigger Property="IsSelected" Value="True">
- <Setter Property="IsSelectedChanged" TargetName="item" Value="true"/>
- </Trigger>
- <Trigger Property="IsEnabled" Value="False">
- <Setter Property="IsEnabledChanged" TargetName="item" Value="true"/>
- </Trigger>
- <Trigger Property="IsMouseOver" Value="True">
- <Setter Property="IsMouseOverChanged" TargetName="item" Value="true"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- <DataTemplate x:Key="ItemTemplate">
- <TextBlock x:Name="txt" Text="{Binding}" Margin="2,0,0,0" />
- <DataTemplate.Triggers>
- <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type local:OwnerDrawComboBox}}}" Value="false">
- <Setter Property="Foreground" TargetName="txt" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
- </DataTrigger>
- </DataTemplate.Triggers>
- </DataTemplate>
- <DataTemplate x:Key="ItemTemplate1">
- <local:OwnerDrawComboBoxItem Value="{Binding}" SnapsToDevicePixels="true"/>
- </DataTemplate>
- <Style TargetType="{x:Type Popup}">
- <Setter Property="MinWidth" Value="{Binding DropDownWidth, RelativeSource={RelativeSource AncestorType={x:Type local:OwnerDrawComboBox}}}"/>
- </Style>
- </ComboBox.Resources>
- </local:OwnerDrawComboBox>
|