Xaml 如何使控件仅在绑定值不是空白时可见?

Xaml 如何使控件仅在绑定值不是空白时可见?,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我有这个XAML: <t:FooterTemplate Text="{Binding SourceFooter }" /> 对象具有边距和颜色 如果SourceFooter的值为“”,如何使用IsVisible使其不可见 我想做一些类似的事情: <t:FooterTemplate Text="{Binding SourceFooter }" IsVisible="{ SourceFooter != "" }" /> 但我知道这是不可能的 第一个选项:转换器 您

我有这个XAML:

<t:FooterTemplate Text="{Binding SourceFooter }" />

对象具有边距和颜色

如果SourceFooter的值为“”,如何使用IsVisible使其不可见

我想做一些类似的事情:

<t:FooterTemplate Text="{Binding SourceFooter }" IsVisible="{ SourceFooter != "" }" />


但我知道这是不可能的

第一个选项:转换器

您可以使用转换器来实现这一点。例如:

 <t:FooterTemplate Text="{Binding SourceFooter }" IsVisible="{Binding SourceFooter, Converter={StaticResource StringEmptyConverter} }" />
如果字符串是空白或null,转换器只返回false

要将其用作静态资源,当然还必须在App.xaml文件的ResourceDictionary中添加一行

            <converters:StringEmptyConverter x:Key="StringEmptyConverter" />
我用什么?

在这种情况下,您应该使用
转换器
解决方案。你可能会在应用程序的其他地方检查字符串是否为空。
在我看来,DataTriggers可以用于更奇特的“触发器”

使用ValueConverter将其绑定到SourceFooter,或者向VM添加一个bool属性,检查SourceFooter是否为空
            <converters:StringEmptyConverter x:Key="StringEmptyConverter" />
<t:FooterTemplate Text="{Binding SourceFooter}" >
 <t:FooterTemplate.Triggers>
   <DataTrigger
    Binding="{Binding SourceFooter}"
    TargetType="t:FooterTemplate"
    Value="">
        <Setter Property="IsVisible" Value="false" />
    </DataTrigger>
 </t:FooterTemplate.Triggers>
</t:FooterTemplate>