【问题标题】:WPF Clear combobox text value when checkbox is unchecked未选中复选框时,WPF清除组合框文本值
【发布时间】:2020-11-22 19:38:28
【问题描述】:

每当取消选中复选框时,我想清除组合框文本值或将其设置为另一个字符串。有没有什么好方法可以在 xaml 中将它们绑定在一起? 谢谢。

【问题讨论】:

  • 您是否获得了可以添加到问题中的 ComboBox 和 Checkbox 的 XAML 和 C# 代码的 sn-p?

标签: c# wpf checkbox combobox binding


【解决方案1】:

也许这会有所帮助。它通过绑定解决:

<Grid>
   <ComboBox IsEditable="{Binding ElementName=Box, Path=IsChecked}" HorizontalAlignment="Left" Margin="211,167,0,0" VerticalAlignment="Top" Width="120">
      <ComboBoxItem>Test</ComboBoxItem>
      <ComboBoxItem>Test2</ComboBoxItem>
   </ComboBox>
   <CheckBox x:Name="Box" Content="Clear" HorizontalAlignment="Left" Margin="131,171,0,0" VerticalAlignment="Top"/>
</Grid>

如果要改变文字,可能需要写一个转换器:

Text="{Binding ElementName=Box, Path=IsChecked, Mode=OneWay, Converter={StaticResource TextConverter}}"

看看Microsoft Documentation关于如何编写一个值转换器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多