在WPF中隐藏ComboBox箭头

时间:2013-03-13 04:28:45

标签: wpf xaml combobox hide

我正在创建一个WPF应用程序,因为我需要隐藏 ComboBox's Arrow符号。请帮助我任何身体。

2 个答案:

答案 0 :(得分:2)

感谢您的更新。 以下链接为我提供了一个简单的解决方案。

Hide ComboBox Arrow

答案 1 :(得分:1)

如果你想这样做,你需要重新设计ComboBox,正如DHN所说。这是我用过的那个:

<!-- ComboBox style -->
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="Height" Value="25"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton 
                    Name="ToggleButton" 
                    Template="{DynamicResource ComboBoxToggleButton}" 
                    Grid.Column="2" 
                    Focusable="false"
                    IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                    ClickMode="Press">
                    </ToggleButton>
                    <ContentPresenter
                        Name="ContentSite"
                        IsHitTestVisible="False" 
                        Content="{TemplateBinding SelectionBoxItem}"
                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                        Margin="3,3,23,3"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left" />
                    <TextBox x:Name="PART_EditableTextBox"
                        Style="{x:Null}" 
                        Template="{Binding Text}" 
                        HorizontalAlignment="Left" 
                        VerticalAlignment="Center"
                        Margin="3,3,23,3"
                        Focusable="True" 
                        Background="Transparent"
                        Visibility="Hidden"
                        Foreground="DarkBlue"
                        IsReadOnly="{TemplateBinding IsReadOnly}"/>
                    <Popup 
                        Name="Popup"
                        Placement="Bottom"
                        IsOpen="{TemplateBinding IsDropDownOpen}"
                        AllowsTransparency="True" 
                        Focusable="False"
                        PopupAnimation="Slide">
                        <Grid 
                          Name="DropDown"
                          SnapsToDevicePixels="True"                
                          MinWidth="{TemplateBinding ActualWidth}"
                          MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border 
                                x:Name="DropDownBorder"
                                Background="{DynamicResource TextBoxBrush}"
                                BorderThickness="1"
                                BorderBrush="{DynamicResource SolidBorderBrush}"/>
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#373737"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                        <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="0,0,4,4"/>
                        <Setter TargetName="DropDownBorder" Property="Margin" Value="0"/>
                    </Trigger>
                    <Trigger Property="IsEditable"
           Value="true">
                        <Setter Property="IsTabStop" Value="false"/>
                        <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
    </Style.Triggers>
</Style>

如果您在此代码中看到,您有 ToggleButton 。这个是定义你的箭头的那个。这里有ToggleButton样式:

<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <Border
          x:Name="Border" 
          Grid.ColumnSpan="2"
          CornerRadius="5"
          Background="{DynamicResource DarkGradient}"
          BorderBrush="{DynamicResource SolidBorderBrush}"
          BorderThickness="1" />
        <Border 
          Grid.Column="0"
          CornerRadius="5,0,0,5"  
          Margin="1" 
          Background="{DynamicResource TextBoxBrush}" 
          BorderBrush="{DynamicResource SolidBorderBrush}"
          BorderThickness="0,0,1,0" />
        <Path 
          x:Name="Arrow"
          Grid.Column="1"     
          Fill="{DynamicResource TextBoxBrush}"
          HorizontalAlignment="Center"
          VerticalAlignment="Center"
          Data="M 0 0 L 4 4 L 8 0 Z"/>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="BorderBrush" Value="Gray" />
            <Setter Property="Foreground" Value="DarkBlue"/>
            <Setter TargetName="Arrow" Property="Fill" Value="DarkGray" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

如果删除此行:

<Path 
      x:Name="Arrow"
      Grid.Column="1"     
      Fill="{DynamicResource TextBoxBrush}"
      HorizontalAlignment="Center"
      VerticalAlignment="Center"
      Data="M 0 0 L 4 4 L 8 0 Z"/>

并评论:

<Setter TargetName="Arrow" Property="Fill" Value="DarkGray" />

ToggleButton上删除你的箭头:)

注意:如果您想要完全使用此代码,则需要创建自己的填充颜色。