如何设置绑定到文本框的标签的可见性
本文关键字:文本 标签 可见性 绑定 何设置 设置 | 更新日期: 2023-09-27 18:19:50
我有三个绑定到LABEL的TEXTBOX。当我在TEXTBOX中键入内容时,TEXTBOX文本值将设置为Label问题是当文本框为空时,我想将LABEL的Visiblity设置为COLLAPSED,反之亦然。如何在WPF中使用可见性转换?
在.XAML文件中:
<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
<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
接口实现:
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();
}
}
现在,在您的资源中的某个位置注册您的转换器:
<converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />
最后,在元素上使用转换器,如下所示:
<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"/>
您可以使用这样的DataTrigger:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=txtEmail1, Path=Text}" Value="">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBox Name="txtEmail1" Text="Email" />
<Label Name="lblContactEmail1" Background="Yellow" Content="{Binding Path=Text, ElementName=txtEmail1}" />
</StackPanel>
在这个仅XAML的解决方案中不需要其他类。当然,您必须根据自己的需要进行调整(例如绑定)。我省略了不必要的属性。