如何设置绑定到文本框的标签的可见性?
Posted
技术标签:
【中文标题】如何设置绑定到文本框的标签的可见性?【英文标题】:How to set Visibility of Label which is bound to Textbox? 【发布时间】:2013-06-21 23:55:07 【问题描述】:我有三个绑定到 LABEL 的 TEXTBOX。当我在 TEXTBOX 中输入内容时,TextBox 文本值设置为标签。 问题是我想在文本框为空白时将标签的可见性设置为折叠,反之亦然。如何在 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 的文本显示为“可见”,而不是在文本框中输入的确切文本。
帮助赞赏! 谢谢!
【问题讨论】:
仔细看看我的例子。您应该不将转换器用于Content
属性,而是用于Visibility
属性,如我的示例所示。 Content
应该保留原来的示例。
检查我对答案所做的编辑。
【参考方案1】:
创建您自己的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"/>
【讨论】:
对不起@Klaus!但是如何使用它?我是 wpf 的新手! 太棒了!这对我帮助很大!谢谢! :)【参考方案2】:您可以像这样使用 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 的解决方案中不需要额外的类。当然,您必须使其适应您的需求(例如绑定)。我省略了不必要的属性。
【讨论】:
以上是关于如何设置绑定到文本框的标签的可见性?的主要内容,如果未能解决你的问题,请参考以下文章