我有三个TEXTBOX绑定到LABEL。当我在TEXTBOX中键入内容时,TextBox文本值设置为Label。 问题是我想在文本框为空白时将LABEL的Visiblity设置为COLLAPSED,反之亦然。如何在WPF中使用Visibility Convert来实现?
<。>在.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的文本显示为“可见”,而不是在文本框中输入的确切文本。
帮助感谢! 谢谢!
答案 0 :(得分:2)
创建自己的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"/>
答案 1 :(得分:0)
你可以像这样使用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的解决方案中无需其他类。当然,你必须根据自己的需要(例如绑定)进行调整。我省略了不必要的属性。