wpf - WPF 中的清除组合框

标签 wpf xaml data-binding

我有一个组合框,只要选中一个复选框,我就想清除它。 我该怎么做?

我的组合框:

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

简约演示:

enter image description here

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

相关文章:

android - 使用数据绑定(bind)和 MVVM 处理 onClick 事件

c# - 如何使用CommandParameter发送列表

c# - TFS C# - 加载 workItemStore 返回 null 或类型初始值设定项异常

wpf - 如何在 FlowDocument 中将表格放在一起?

wpf - 使用 Storyboard 在动画期间更改图像

.net - 我应该将什么类型的对象绑定(bind)到 n 层应用程序中的 WPF 表单?

c# - Application.ThreadException 事件未捕获绑定(bind)属性的 Set 方法中的异常

wpf - 用于测试目的的简单 ItemsSource

c# - 有没有办法将 Controls.ContextMenu 转换为 Forms.ContextMenu?

c# - 构建基于 WPF Ribbon 的应用程序的步骤