是的,你說得對。你可以寫一個轉換器,像這樣:
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}}" />
非常感謝!我認爲它可以用更少的代碼!有沒有辦法只在XAML中做?謝謝 –
@WalterFabioSimoni不幸的是沒有。 C#(或VB.NET)必須在某個時候涉足。 – vcsjones