0

我有三个绑定到 LABEL 的 TEXTBOX。当我在 TEXTBOX 中输入内容时,TextBox 文本值设置为标签。问题是我想在文本框为空白时将标签的可见性设置为折叠,反之亦然。如何使用 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 的文本显示为“可见”,而不是在文本框中输入的确切文本。

帮助赞赏!谢谢!

4

2 回答 2

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"/>
于 2013-06-25T08:56:50.380 回答
1

您可以像这样使用 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 的解决方案中不需要额外的类。当然,您必须使其适应您的需求(例如绑定)。我省略了不必要的属性。

于 2013-06-25T09:08:50.923 回答