You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF技术问题:TextBox为空时移除ComboBox选中项

实现TextBox为空时禁用ComboBox,且清空TextBox后保留ComboBox选中状态

已实现的交互逻辑:

  • 当TextBox内容为空时,自动禁用ComboBox
  • 选中ComboBox选项后清空TextBox内容,ComboBox的选中项仍会正常显示

对应的XAML代码如下:

<TextBox Name="txt0"  PreviewTextInput="NumericOnly" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="70" Margin="52,83,0,0"/>
<ComboBox Name="cbo0" ItemsSource="{Binding Source={StaticResource enumValues}}" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="77" Margin="144,84,0,0">
    <ComboBox.Style>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="IsEnabled" Value="True" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=txt0, Path=Text}"  Value="">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

内容的提问来源于stack exchange,提问作者refaelo1979

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:35:06