如何隐藏combobox中的下拉箭头按钮且不对其进行禁用?
ComboBox 隐藏下拉箭头按钮实现方案
你目前已经可以通过 IsDropDownOpen = true; 代码主动触发下拉框展开,隐藏箭头按钮(非禁用)的实现方式根据你使用的UI框架不同有所区别:
WPF 平台
重写 ComboBox 的默认控件模板,找到模板内名为 ToggleButton 的箭头触发按钮,将其 Visibility 属性设为 Collapsed 即可,不会影响你原有代码控制 IsDropDownOpen 展开下拉的逻辑。
示例样式代码:
<Style TargetType="{x:Type ComboBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Grid> <ContentPresenter Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Margin="6,0,0,0" VerticalAlignment="Center"/> <!-- 箭头按钮直接设为隐藏 --> <ToggleButton x:Name="ToggleButton" Visibility="Collapsed" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/> <Popup x:Name="PART_Popup" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" PopupAnimation="Slide"> <Border Background="White" BorderBrush="LightGray" BorderThickness="1"> <ScrollViewer> <ItemsPresenter/> </ScrollViewer> </Border> </Popup> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
WinUI / UWP 平台
修改 ComboBox 的默认样式,找到名为 DropDownGlyph 的箭头图标元素,设置其 Opacity = 0 或者 Visibility = Collapsed 即可。
WinForms 平台
通过重写绘制逻辑覆盖箭头区域即可,自定义 ComboBox 类示例:
public class NoArrowComboBox : ComboBox { private const int WM_PAINT = 0x000F; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_PAINT) { using (var g = Graphics.FromHwnd(Handle)) { var arrowArea = new Rectangle( ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth, 0, SystemInformation.VerticalScrollBarWidth, ClientRectangle.Height ); g.FillRectangle(new SolidBrush(BackColor), arrowArea); } } } }
箭头按钮位置参考:
内容的提问来源于stack exchange,提问作者AnonymousKKYY
相关产品推荐
相关产品推荐

