ComboBox.xaml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <local:OwnerDrawComboBox x:Class="CustomControls.ComboBox"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:CustomControls">
  5. <ComboBox.Resources>
  6. <SolidColorBrush x:Key="ComboBox.Static.Background" Color="White"/>
  7. <SolidColorBrush x:Key="ComboBox.Static.Border" Color="#FFACACAC"/>
  8. <SolidColorBrush x:Key="ComboBox.Static.Glyph" Color="#FF606060"/>
  9. <LinearGradientBrush x:Key="ComboBox.MouseOver.Background" EndPoint="0,1" StartPoint="0,0">
  10. <GradientStop Color="#FFECF4FC" Offset="0.0"/>
  11. <GradientStop Color="#FFDCECFC" Offset="1.0"/>
  12. </LinearGradientBrush>
  13. <SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="#FF7EB4EA"/>
  14. <SolidColorBrush x:Key="ComboBox.MouseOver.Glyph" Color="#FF000000"/>
  15. <SolidColorBrush x:Key="ComboBox.Disabled.Background" Color="#FFF0F0F0"/>
  16. <SolidColorBrush x:Key="ComboBox.Disabled.Border" Color="#FFD9D9D9"/>
  17. <SolidColorBrush x:Key="ComboBox.Disabled.Glyph" Color="#FFBFBFBF"/>
  18. <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
  19. <Setter Property="OverridesDefaultStyle" Value="true"/>
  20. <Setter Property="IsTabStop" Value="false"/>
  21. <Setter Property="Focusable" Value="false"/>
  22. <Setter Property="ClickMode" Value="Press"/>
  23. <Setter Property="Template">
  24. <Setter.Value>
  25. <ControlTemplate TargetType="{x:Type ToggleButton}">
  26. <Border x:Name="templateRoot" Background="{StaticResource ComboBox.Static.Background}" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
  27. <Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="19">
  28. <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"/>
  29. </Border>
  30. </Border>
  31. <ControlTemplate.Triggers>
  32. <Trigger Property="IsMouseOver" Value="true">
  33. <Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Background}"/>
  34. <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Border}"/>
  35. </Trigger>
  36. <Trigger Property="IsEnabled" Value="false">
  37. <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource ComboBox.Disabled.Glyph}"/>
  38. <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Background}"/>
  39. <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Border}"/>
  40. </Trigger>
  41. </ControlTemplate.Triggers>
  42. </ControlTemplate>
  43. </Setter.Value>
  44. </Setter>
  45. </Style>
  46. <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
  47. <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
  48. <Grid.ColumnDefinitions>
  49. <ColumnDefinition Width="*"/>
  50. <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
  51. </Grid.ColumnDefinitions>
  52. <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}}">
  53. <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}">
  54. <ScrollViewer x:Name="DropDownScrollViewer">
  55. <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  56. </ScrollViewer>
  57. </Border>
  58. </Popup>
  59. <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}"/>
  60. <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}"/>
  61. <local:OwnerDrawComboBoxItem x:Name="nullItem" Visibility="Hidden" IsHitTestVisible="False" Margin="{TemplateBinding Padding}"/>
  62. </Grid>
  63. <ControlTemplate.Triggers>
  64. <Trigger Property="SelectedIndex" Value="-1">
  65. <Setter Property="Visibility" TargetName="nullItem" Value="Visible"/>
  66. <Setter Property="Visibility" TargetName="contentPresenter" Value="Collapsed"/>
  67. <Setter Property="IsSelectedChanged" TargetName="nullItem" Value="true"/>
  68. </Trigger>
  69. </ControlTemplate.Triggers>
  70. </ControlTemplate>
  71. <SolidColorBrush x:Key="TextBox.Static.Background" Color="#FFFFFFFF"/>
  72. <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
  73. <Setter Property="OverridesDefaultStyle" Value="true"/>
  74. <Setter Property="AllowDrop" Value="true"/>
  75. <Setter Property="MinWidth" Value="0"/>
  76. <Setter Property="MinHeight" Value="0"/>
  77. <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
  78. <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
  79. <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
  80. <Setter Property="Template">
  81. <Setter.Value>
  82. <ControlTemplate TargetType="{x:Type TextBox}">
  83. <ScrollViewer x:Name="PART_ContentHost" Background="Transparent" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
  84. </ControlTemplate>
  85. </Setter.Value>
  86. </Setter>
  87. </Style>
  88. <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
  89. <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
  90. <Grid.ColumnDefinitions>
  91. <ColumnDefinition Width="*"/>
  92. <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
  93. </Grid.ColumnDefinitions>
  94. <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
  95. <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}">
  96. <ScrollViewer x:Name="DropDownScrollViewer">
  97. <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
  98. <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
  99. <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
  100. </Canvas>
  101. <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  102. </Grid>
  103. </ScrollViewer>
  104. </Border>
  105. </Popup>
  106. <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}"/>
  107. <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}"/>
  108. </Grid>
  109. <ControlTemplate.Triggers>
  110. <Trigger Property="IsEnabled" Value="false">
  111. <Setter Property="Opacity" TargetName="textBox" Value="0.5"/>
  112. </Trigger>
  113. <Trigger Property="HasItems" Value="false">
  114. <Setter Property="Height" TargetName="dropDownBorder" Value="95"/>
  115. </Trigger>
  116. </ControlTemplate.Triggers>
  117. </ControlTemplate>
  118. <Style x:Key="ItemContainerStyle1" TargetType="{x:Type ComboBoxItem}">
  119. <Setter Property="Template">
  120. <Setter.Value>
  121. <ControlTemplate TargetType="{x:Type ComboBoxItem}">
  122. <local:OwnerDrawComboBoxItem x:Name="item" SnapsToDevicePixels="true"/>
  123. <ControlTemplate.Triggers>
  124. <Trigger Property="IsSelected" Value="True">
  125. <Setter Property="IsSelectedChanged" TargetName="item" Value="true"/>
  126. </Trigger>
  127. <Trigger Property="IsEnabled" Value="False">
  128. <Setter Property="IsEnabledChanged" TargetName="item" Value="true"/>
  129. </Trigger>
  130. <Trigger Property="IsMouseOver" Value="True">
  131. <Setter Property="IsMouseOverChanged" TargetName="item" Value="true"/>
  132. </Trigger>
  133. </ControlTemplate.Triggers>
  134. </ControlTemplate>
  135. </Setter.Value>
  136. </Setter>
  137. </Style>
  138. <DataTemplate x:Key="ItemTemplate">
  139. <TextBlock x:Name="txt" Text="{Binding}" Margin="2,0,0,0" />
  140. <DataTemplate.Triggers>
  141. <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type local:OwnerDrawComboBox}}}" Value="false">
  142. <Setter Property="Foreground" TargetName="txt" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
  143. </DataTrigger>
  144. </DataTemplate.Triggers>
  145. </DataTemplate>
  146. <DataTemplate x:Key="ItemTemplate1">
  147. <local:OwnerDrawComboBoxItem Value="{Binding}" SnapsToDevicePixels="true"/>
  148. </DataTemplate>
  149. <Style TargetType="{x:Type Popup}">
  150. <Setter Property="MinWidth" Value="{Binding DropDownWidth, RelativeSource={RelativeSource AncestorType={x:Type local:OwnerDrawComboBox}}}"/>
  151. </Style>
  152. </ComboBox.Resources>
  153. </local:OwnerDrawComboBox>