是的,你是对的。您可以像这样编写转换器:
public class StringLengthVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
var strValue = value as String;
return string.IsNullOrEmpty(strValue) ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
//We can't support this
throw new NotImplementedException();
}
}
要使用转换器,请将资源添加到您的页面:
<phone:PhoneApplicationPage.Resources>
<local:StringLengthVisibilityConverter x:Key="LengthConverter" />
</phone:PhoneApplicationPage.Resources>
替换local
指向转换器的 CLR 命名空间的 xmlns。
之后,我们可以在电网中使用我们的转换器:
<Grid Visibility="{Binding Path=m_strMail, Converter={StaticResource LengthConverter}}" />