ToolStripComboBox.xaml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <local:OwnerDrawComboBox x:Class="CustomControls.ToolStripComboBox"
  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.Foreground" Color="#FFBFBFBF"/>
  18. <SolidColorBrush x:Key="ComboBox.Disabled.Glyph" Color="#FFBFBFBF"/>
  19. <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
  20. <Setter Property="OverridesDefaultStyle" Value="true"/>
  21. <Setter Property="IsTabStop" Value="false"/>
  22. <Setter Property="Focusable" Value="false"/>
  23. <Setter Property="ClickMode" Value="Press"/>
  24. <Setter Property="Template">
  25. <Setter.Value>
  26. <ControlTemplate TargetType="{x:Type ToggleButton}">
  27. <Border x:Name="templateRoot" Background="{DynamicResource ComboBox.Static.Background}" BorderBrush="{DynamicResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
  28. <Border x:Name="splitBorder" Background="{DynamicResource ComboBox.Static.Background}" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="14">
  29. <Path x:Name="arrow" Data="M 0 0 L 3 3 L 6 0 Z" Fill="{DynamicResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
  30. </Border>
  31. </Border>
  32. <ControlTemplate.Triggers>
  33. <Trigger Property="IsMouseOver" Value="true">
  34. <Setter Property="Background" TargetName="splitBorder" Value="{DynamicResource ComboBox.MouseOver.Background}"/>
  35. <Setter Property="BorderBrush" TargetName="templateRoot" Value="{DynamicResource ComboBox.MouseOver.Border}"/>
  36. </Trigger>
  37. <Trigger Property="IsEnabled" Value="false">
  38. <Setter Property="Fill" TargetName="arrow" Value="{DynamicResource ComboBox.Disabled.Glyph}"/>
  39. <Setter Property="Background" TargetName="templateRoot" Value="{DynamicResource ComboBox.Disabled.Background}"/>
  40. <Setter Property="Background" TargetName="splitBorder" Value="{DynamicResource ComboBox.Disabled.Background}"/>
  41. <Setter Property="BorderBrush" TargetName="templateRoot" Value="{DynamicResource ComboBox.Disabled.Border}"/>
  42. </Trigger>
  43. </ControlTemplate.Triggers>
  44. </ControlTemplate>
  45. </Setter.Value>
  46. </Setter>
  47. </Style>
  48. <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
  49. <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
  50. <Grid.ColumnDefinitions>
  51. <ColumnDefinition Width="*"/>
  52. <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
  53. </Grid.ColumnDefinitions>
  54. <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" Placement="Bottom" MinWidth="{Binding DropDownWidth, RelativeSource={RelativeSource AncestorType={x:Type local:OwnerDrawComboBox}}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
  55. <Border x:Name="dropDownBorder" Background="{DynamicResource ComboBox.Static.Background}" BorderBrush="{DynamicResource ComboBox.MouseOver.Border}" BorderThickness="1" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
  56. <ScrollViewer x:Name="DropDownScrollViewer">
  57. <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  58. </ScrollViewer>
  59. </Border>
  60. </Popup>
  61. <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}"/>
  62. <ContentPresenter x:Name="contentPresenter" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" HorizontalAlignment="Stretch" IsHitTestVisible="false" Margin="2,3,2,3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
  63. </Grid>
  64. </ControlTemplate>
  65. <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
  66. <Setter Property="OverridesDefaultStyle" Value="true"/>
  67. <Setter Property="AllowDrop" Value="true"/>
  68. <Setter Property="MinWidth" Value="0"/>
  69. <Setter Property="MinHeight" Value="0"/>
  70. <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
  71. <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
  72. <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
  73. <Setter Property="Template">
  74. <Setter.Value>
  75. <ControlTemplate TargetType="{x:Type TextBox}">
  76. <ScrollViewer x:Name="PART_ContentHost" Background="Transparent" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
  77. </ControlTemplate>
  78. </Setter.Value>
  79. </Setter>
  80. </Style>
  81. <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type local:OwnerDrawComboBox}">
  82. <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
  83. <Grid.ColumnDefinitions>
  84. <ColumnDefinition Width="*"/>
  85. <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
  86. </Grid.ColumnDefinitions>
  87. <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" MinWidth="{Binding DropDownWidth, RelativeSource={RelativeSource AncestorType={x:Type local:OwnerDrawComboBox}}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
  88. <Border x:Name="dropDownBorder" Background="{DynamicResource ComboBox.Static.Background}" BorderBrush="{DynamicResource ComboBox.MouseOver.Border}" BorderThickness="1" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
  89. <ScrollViewer x:Name="DropDownScrollViewer">
  90. <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
  91. <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
  92. <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
  93. </Canvas>
  94. <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  95. </Grid>
  96. </ScrollViewer>
  97. </Border>
  98. </Popup>
  99. <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}"/>
  100. <Border x:Name="border" Background="Transparent" Margin="{TemplateBinding BorderThickness}">
  101. <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}"/>
  102. </Border>
  103. </Grid>
  104. <ControlTemplate.Triggers>
  105. <Trigger Property="IsEnabled" Value="false">
  106. <Setter Property="Opacity" TargetName="border" Value="0.5"/>
  107. </Trigger>
  108. <Trigger Property="HasItems" Value="false">
  109. <Setter Property="Height" TargetName="dropDownBorder" Value="95"/>
  110. </Trigger>
  111. </ControlTemplate.Triggers>
  112. </ControlTemplate>
  113. <Style x:Key="ItemContainerStyle1" TargetType="{x:Type ComboBoxItem}">
  114. <Setter Property="Template">
  115. <Setter.Value>
  116. <ControlTemplate TargetType="{x:Type ComboBoxItem}">
  117. <local:OwnerDrawComboBoxItem x:Name="item" SnapsToDevicePixels="true"/>
  118. <ControlTemplate.Triggers>
  119. <Trigger Property="IsSelected" Value="True">
  120. <Setter Property="IsSelectedChanged" TargetName="item" Value="true"/>
  121. </Trigger>
  122. <Trigger Property="IsEnabled" Value="False">
  123. <Setter Property="IsEnabledChanged" TargetName="item" Value="true"/>
  124. </Trigger>
  125. <Trigger Property="IsMouseOver" Value="True">
  126. <Setter Property="IsMouseOverChanged" TargetName="item" Value="true"/>
  127. </Trigger>
  128. </ControlTemplate.Triggers>
  129. </ControlTemplate>
  130. </Setter.Value>
  131. </Setter>
  132. </Style>
  133. <DataTemplate x:Key="ItemTemplate">
  134. <TextBlock x:Name="txt" Text="{Binding}" Margin="2,0,0,0" />
  135. <DataTemplate.Triggers>
  136. <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type local:OwnerDrawComboBox}}}" Value="false">
  137. <Setter Property="Foreground" TargetName="txt" Value="{DynamicResource ComboBox.Disabled.Foreground}"/>
  138. </DataTrigger>
  139. </DataTemplate.Triggers>
  140. </DataTemplate>
  141. <DataTemplate x:Key="ItemTemplate1">
  142. <local:OwnerDrawComboBoxItem Value="{Binding}" SnapsToDevicePixels="true"/>
  143. </DataTemplate>
  144. </ComboBox.Resources>
  145. </local:OwnerDrawComboBox>