Android 为什么我的DataTrigger在我的Xamarin表单应用程序中不起作用?
根据Xamarins网站关于Android 为什么我的DataTrigger在我的Xamarin表单应用程序中不起作用?,android,ios,xaml,xamarin,Android,Ios,Xaml,Xamarin,根据Xamarins网站关于DataTriggers的文档,我想在我的ListView中实现一个解决方案,检查标签中是否有任何文本。如果Text.Length为0,则根据我的代码使用Setters设置BoxView的样式 <Label x:Name="forward_label" Text="{Binding next_charterer_info}" /> <BoxView x:Name="forward_alert" BackgroundColor="
DataTriggers
的文档,我想在我的ListView中实现一个解决方案,检查标签中是否有任何文本。如果Text.Length
为0,则根据我的代码使用Setters
设置BoxView
的样式
<Label x:Name="forward_label" Text="{Binding next_charterer_info}" />
<BoxView x:Name="forward_alert"
BackgroundColor="Red">
<BoxView.Triggers>
<DataTrigger TargetType="BoxView"
Binding="{Binding Source={x:Reference forward_label},
Path=Text.Length}" Value="0">
<Setter Property="BackgroundColor" Value="White" />
</DataTrigger>
</BoxView.Triggers>
</BoxView>
如您所见,我有一个标签,它被检查为text.length
,如果它为0,则BoxView
的BackgroundColor
设置为白色。但是,当我运行应用程序时,它不工作。这一条件似乎从未得到满足。绑定数据要么有值,要么返回空字符串
有谁能帮助我解决这种方法的错误。您是否尝试过使用值转换器,我认为这可能是一个更简单的解决方案
using Xamarin.Forms;
namespace YourProject.Converters
{
public class StringLengthToColorConverter : IValueConverter
{
public Color EmptyColor { get; set; }
public Color DefaultColor { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || String.IsNullOrEmpty(value.ToString()))
{
return EmptyColor;
}
return DefaultColor;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
你的页面应该看起来像
<Page.Resources>
<ResourceDictionary>
<conv:StringLengthToColorConverter x:Key="StringLengthToColorConverter" EmptyColor="White" DefaultColor="Red" />
</ResourceDictionary>
</Page.Resources>
<Label x:Name="forward_label" Text="{Binding next_charterer_info}" />
<BoxView x:Name="forward_alert"
BackgroundColor="{Binding next_charterer_info,Converter={StaticResource StringLengthToColorConverter}}">
</BoxView>