#495 – 绑定Visibility 属性不需要转换器类(Binding to a Visibility Property Without Using a Value Converter)

本文介绍如何在WPF中直接绑定ComboBox的选择项内容到Label的Visibility属性,无需使用转换器。通过实例展示了如何根据ComboBox的选择改变中间Label的可见性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在绑定Visibility属性的时候,你可以使用一个转换器converter将string类型转换为System.Windows.Visibility 类型。你也可以直接将string类型设置给Visibility 属性,不需要转换器。

下面的例子中。我们绑定选中的ComboBoxItemContent 属性。Content 里面包含的是字符串,绑定的过程中不需要转换转换器。

<StackPanel>
    <StackPanel Orientation="Horizontal" Margin="10">
        <Label Content="Snoopy" Margin="3" Background="BurlyWood"/>
        <Label Content="Waldo" Margin="3" Background="Thistle"
               Visibility="{Binding ElementName=cboVisibility, Path=SelectedValue.Content}"/>
        <Label Content="Dagwood" Margin="3" Background="LightGreen"/>
    </StackPanel>
    <ComboBox Name="cboVisibility" HorizontalAlignment="Center" SelectedIndex="0">
        <ComboBox.Items>
            <ComboBoxItem Content="Visible"/>
            <ComboBoxItem Content="Collapsed"/>
            <ComboBoxItem Content="Hidden"/>
        </ComboBox.Items>
    </ComboBox>
    <Label Content="Select visibility of middle Label" HorizontalAlignment="Center"/>
</StackPanel>

原文地址:https://wpf.2000things.com/2012/02/15/495-binding-to-a-visibility-property-without-using-a-value-converter/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值