一个自定义的WPF ComboBox模板

来源:互联网 发布:eclipse js提示插件 编辑:程序博客网 时间:2024/04/29 14:52
<ComboBox Name="filterCom" Height="42" SelectedIndex="0" ItemsSource="{Binding SeriesInfos}" SelectionChanged="FilterCom_SelectionChanged">            <ComboBox.Resources>                <!--修改选中项的背景色-->                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF222222"/>                <converter:BoolVisibleConverter x:Key="boolVisibleConverter"/>            </ComboBox.Resources>            <ComboBox.Style>                <Style TargetType="{x:Type ComboBox}">                    <Setter Property="Template">                        <Setter.Value>                            <ControlTemplate TargetType="{x:Type ComboBox}">                                <Grid Background="#FF222222">                                    <Grid.ColumnDefinitions>                                        <ColumnDefinition Width="Auto"/>                                        <ColumnDefinition Width="*"/>                                    </Grid.ColumnDefinitions>                                    <Image Source="../../Icons/arrow_White.png" Width="8" Margin="8,0"                                            Visibility="{Binding ElementName=filterCom, Path=IsEnabled, Converter={StaticResource boolVisibleConverter}}"/>                                    <!-- ToggleButton 已数据绑定到 ComboBox 本身以切换 IsDropDownOpen -->                                    <ToggleButton Grid.ColumnSpan="2" x:Name="ToggleButton" Focusable="false"                                                   IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">                                        <ToggleButton.Template>                                            <ControlTemplate TargetType="{x:Type ToggleButton}">                                                <Border Background="Transparent"/>                                            </ControlTemplate>                                        </ToggleButton.Template>                                    </ToggleButton>                                    <ContentPresenter Grid.Column="1" x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center"                                                      Content="{TemplateBinding SelectionBoxItem}" IsHitTestVisible="False"                                                      ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/>                                                                        <!-- 可编辑模式下,将 TextBox 命名为 PART_EditableTextBox,否则 ComboBox 将无法识别它 -->                                    <!--<TextBox Visibility="Hidden" Template="{DynamicResource ComboBoxTextBox}" HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" Background="Transparent" IsReadOnly="{TemplateBinding IsReadOnly}"/>-->                                    <!-- Popup 可显示 ComboBox 中的项列表。IsOpen 已数据绑定到通过 ComboBoxToggleButton 来切换的 IsDropDownOpen -->                                    <Popup x:Name="Popup" IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Left" Focusable="False" AllowsTransparency="True" PopupAnimation="Fade">                                        <Border x:Name="DropDown" BorderBrush="LightGray" BorderThickness="1" Background="White" CornerRadius="6"                                                    MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}">                                            <ScrollViewer x:Name="scrViewer" Margin="3,6,3,6" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" CanContentScroll="True">                                                <!-- StackPanel 用于显示子级,方法是将 IsItemsHost 设置为 True -->                                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" Width="{Binding ElementName=scrViewer, Path=ActualWidth}" HorizontalAlignment="Center"/>                                            </ScrollViewer>                                        </Border>                                    </Popup>                                </Grid>                            </ControlTemplate>                        </Setter.Value>                    </Setter>                    <Setter Property="ItemTemplate">                        <Setter.Value>                            <DataTemplate>                                <TextBlock Grid.Column="1" Margin="0,5" Text="{Binding SeriesName}" FontSize="15" Foreground="Gray" FontWeight="Bold"                                           TextAlignment="Center" HorizontalAlignment="Center"/>                            </DataTemplate>                        </Setter.Value>                    </Setter>                </Style>            </ComboBox.Style>        </ComboBox>

0 0
原创粉丝点击