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
相关产品推荐
相关产品推荐

