0

If style definition is:

<Style x:Key="BodyTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BaselineTextStyle}">
    <Setter Property="FontWeight" Value="SemiLight"/>
</Style>

How do I add property that would collapse the TextBlock if the value is null?

Thanks.

4

2 回答 2

2

感谢内特的意见。最终解决方案如下所示。

首先创建转换器。

public class TextIsNullOrEmptyToVisibilityConverter : IValueConverter
{
public object IValueConverter.Convert(object value, System.Type targetType,   object parameter, string language)
    {
    string Str = (string)value;
    if (string.IsNullOrEmpty(Str)) {
        return Visibility.Collapsed;
    } else {
        return Visibility.Visible;
    }
}

public object IValueConverter.ConvertBack(object value, System.Type targetType, object parameter, string language)
{
    throw new NotImplementedException();
}
}

在页面 XAML 文件中为转换器提供资源键。

<Page.Resources>
    <local:TextIsNullOrEmptyToVisibilityConverter x:Key="visiblityConverter"/>
</Page.Resources>

如上面 Nate 所述,将资源应用于可见性属性。

<TextBlock Visibility="{Binding RelativeSource={RelativeSource Self}, Path=Text,
           Converter={StaticResource TextIsNullOrEmptyToVisibilityConverter}}"/>
于 2013-11-13T21:02:35.857 回答
2
<TextBlock Visibility="{Binding RelativeSource={RelativeSource Self}, Path=Text,
               Converter={StaticResource TextIsNullOrEmptyToVisibilityConverter}}"/>

然后,创建一个IValueConverter命名TextIsNullOrEmptyToVisibilityConverter,它接受 astring并返回一个Visibility基于 的枚举string.IsNullOrEmpty

于 2013-11-13T17:20:00.380 回答