在 WPF 中隐藏组合框箭头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15377093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:03:27  来源:igfitidea点击:

Hide ComboBox Arrow in WPF

wpfxamlcomboboxhide

提问by Ramesh

I am creating an WPF application, in that I need to Hidethe ComboBox's Arrowsymbol. Please help me any body.

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

回答by Sonhja

If you want to do that, you need to Restyle the ComboBox, as DHN says. This is the one I used:

如果你想这样做,你需要重新设计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>

If you see in this code, you have a ToggleButton. This one is the one that defines your arrow. Here you have the ToggleButtonstyle:

如果你在这段代码中看到,你有一个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>

If you erase this line:

如果您删除此行:

<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"/>

And also comment:

还有评论:

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

on the ToggleButtonyou erase your arrow :)

ToggleButton你擦掉你的箭头:)

NOTE: If you want to use this code exactly, you need to create your own Fill colors.

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