我有一个组合框,只要选中一个复选框,我就想清除它。 我该怎么做?
我的组合框:
<ComboBox
DisplayMemberPath="KommuneNavn"
SelectedValuePath="KommuneNr"
ItemsSource="{Binding KommuneNavne}"
SelectedValue="{Binding kommuneNr, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Margin="3"
IsEnabled="{Binding IsUdenlandskAdresse, Converter={StaticResource BooleanNotConverter}}"/>
我的复选框绑定(bind)到我的 View 模型中的 bool 属性 IsUdenlandskAdresse
:
<CheckBox Margin="3" IsChecked="{Binding IsUdenlandskAdresse, Mode=TwoWay}"/>
因此,当 IsUdenlandskadresse
设置为 true 时,我希望组合框变为空白。
最佳答案
如果我理解您正在尝试正确执行的操作,您希望 ComoBox 在禁用时为空白(或至少看起来为空白)。执行此操作的最简单方法是在使用样式禁用 ComboBox 时将 Foreground
(用于文本的颜色)更改为 Transparent
。这样您就不需要任何代码隐藏,可以在其他 ComboBox 上重用该行为,并且如果它被重新启用,您也不会丢失选择。
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
简约演示:
<ComboBox Height="Auto" IsEnabled="{Binding ElementName=cckEnabled, Path=IsChecked}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBoxItem>Entry 1</ComboBoxItem>
<ComboBoxItem>Entry 2</ComboBoxItem>
<ComboBoxItem>Entry 3</ComboBoxItem>
<ComboBoxItem>Entry 4</ComboBoxItem>
<ComboBoxItem>Entry 5</ComboBoxItem>
</ComboBox>
<CheckBox Name="cckEnabled" Content="Enabled"/>
关于wpf - WPF 中的清除组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48947667/