如何设置绑定到文本框的标签的可见性? [英] How to set Visibility of Label which is bound to Textbox?

查看:30
本文介绍了如何设置绑定到文本框的标签的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个绑定到 LABEL 的 TEXTBOX.当我在 TEXTBOX 中键入内容时,TextBox 文本值设置为 Label.问题是我想在文本框为空白时将 LABEL 的可见性设置为 COLLAPSED,反之亦然.如何使用 WPF 中的 Visibility Convert 来实现?

I have three TEXTBOX which are bound to LABEL. When I type something in TEXTBOX then TextBox text value is set to Label. Problem is i want to set Visiblity of LABEL to COLLAPSED when text box is blank and vice versa. how to do it using Visibility Convert in WPF?

在 .XAML 文件中:

in .XAML file:

<TextBox Name="txtEmail1" Grid.Column="1" Grid.Row="0" Text="Email" HorizontalAlignment="Stretch" Margin="2" VerticalAlignment="Stretch"/>
<TextBox Name="txtEmail2" Grid.Column="1" Grid.Row="0" Text="Email2" Visibility="Collapsed" Margin="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<TextBox Name="txtEmail3" Grid.Column="1" Grid.Row="0" Text="Email3" Visibility="Collapsed" Margin="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

<Label Name="lblContactEmail1" Content="{Binding Path=Text, ElementName=txtEmail1, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<Label Name="lblContactEmail2" Content="{Binding Path=Text, ElementName=txtEmail2, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<Label Name="lblContactEmail3" Content="{Binding Path=Text, ElementName=txtEmail3, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>

我尝试过:使用下面的类 StringToVisibilityConverter.cs

I have Tried as: Using below class StringToVisibilityConverter.cs

<UserControl xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"  x:Class="XtremeProcurementWPF.UserControls.usContactForm"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:cv="clr-namespace:MyWPF"
             mc:Ignorable="d">
<UserControl.Resources>
        <cv:StringToVisibilityConverter x:Key="visibilityconverter" />
    </UserControl.Resources>
<Grid>
<TextBox Name="txtEmail1" Grid.Column="1" Grid.Row="0"  Text="Email" HorizontalAlignment="Stretch" Margin="2" VerticalAlignment="Stretch" />
    <Label Name="lblContactEmail1" Content="{Binding Path=Text, ElementName=txtEmail1, Mode=OneWay, UpdateSourceTrigger=PropertyChanged,Converter={StaticResource visibilityconverter}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
</UserControl>

问题:将 LABEL 的文本显示为可见",而不是在文本框中输入的确切文本.

感谢帮助!谢谢!

推荐答案

创建您自己的 IValueConverter 接口:

Create your own implementation of the IValueConverter interface:

public class StringToVisibilityConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = value as string;

        if (string.IsNullOrWhiteSpace(s))
            return Visibility.Collapsed;

        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

现在在您的资源中的某处注册您的转换器:

Now somewhere in your resources you register your converter:

<converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />

最后,您在元素上使用转换器,如下所示:

And finally, you use the converter on the element, like this:

<Label Name="lblContactEmail3" 
Visibility="{Binding Path=Text, ElementName=txtEmail3, Converter={StaticResource StringToVisibilityConverter}}" ... />

这是Label的完整代码:

<Label Name="lblContactEmail1" Content="{Binding Path=Text, ElementName=txtEmail1, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding Path=Text, ElementName=txtEmail1, Converter={StaticResource visibilityconverter}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>

这篇关于如何设置绑定到文本框的标签的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆