C# Xamarin仅在解决绑定后才能形成可见的匹配标签范围
我有一些带有格式化文本的标签,其中包含一些像这样的跨距C# Xamarin仅在解决绑定后才能形成可见的匹配标签范围,c#,xaml,data-binding,xamarin.forms,C#,Xaml,Data Binding,Xamarin.forms,我有一些带有格式化文本的标签,其中包含一些像这样的跨距 <Label> <Label.FormattedText> <FormattedString> <Span Text="Size: "/> <Span FontAttributes="Bold" Text="{Binding Item.Size
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Size: "/>
<Span FontAttributes="Bold" Text="{Binding Item.Size , Mode=OneWay}"/>
<Span FontAttributes="Bold" Text="{Binding Item.Unit, Mode=OneWay}"/>
</FormattedString>
</Label.FormattedText>
</Label>
它看起来是这样的:size:32oz。我希望只有当从数据库解析上下文属性时,“size:”一词才会出现。您可以使用转换器隐藏整个标签。我假设
项.Size
是一个字符串,但是在转换器中,您可以转换为正确的类型。这是一个样本
public class OzViewConverter : IValueConverter
{
#region IValueConverter implementation
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var test = value as string; //here you can change the cast, depending on type object you are Binding
if (!string.IsNullOrEmpty(test))
{
return true;
}
return false;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
#endregion
}
然后,在您的页面中
<ContentPage.Resources>
<ResourceDictionary>
<converter:OzViewConverter x:Key="OzViewConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Label IsVisible="{Binding Item.Unit, Converter={StaticResource OzViewConverter}}">
<Label.FormattedText>
<FormattedString>
<Span Text="Size: "/>
<Span FontAttributes="Bold" Text="{Binding Item.Size , Mode=OneWay}"/>
<Span FontAttributes="Bold" Text="{Binding Item.Unit, Mode=OneWay}"/>
</FormattedString>
</Label.FormattedText>
</Label>
您可以使用转换器代替,但我无法格式化它。我也可以使用stringformat,但我希望每个span使用不同的格式。您要隐藏整个标签
还是只隐藏文本大小?假设您要在此处隐藏整个标签。创建一个类似于ValueToVisibilityConverter
的转换器,并将其绑定到标签的IsVisible
属性。这应该行得通。