Xamarin.forms 如何在xamarin表单的列表视图中更改自定义渲染标签textcolor
我们正在开发一个小应用程序,我们使用自定义渲染创建了仪表板,但我无法更改标签文本颜色。它默认显示为标签文本颜色白色,列表视图背景颜色,它将通过api显示,因此,如果是白色背景,则标签文本颜色无法看到。这里我附上了下面的代码。给我一些建议来解决这个问题 菜单控件自定义渲染Xamarin.forms 如何在xamarin表单的列表视图中更改自定义渲染标签textcolor,xamarin.forms,visual-studio-2019,Xamarin.forms,Visual Studio 2019,我们正在开发一个小应用程序,我们使用自定义渲染创建了仪表板,但我无法更改标签文本颜色。它默认显示为标签文本颜色白色,列表视图背景颜色,它将通过api显示,因此,如果是白色背景,则标签文本颜色无法看到。这里我附上了下面的代码。给我一些建议来解决这个问题 菜单控件自定义渲染 public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create<MenuControl,
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create<MenuControl, IEnumerable>(
view => view.ItemsSource,
null,
BindingMode.TwoWay,
null,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
((MenuControl)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
}
);
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty, value);
}
}
公共静态只读BindableProperty项资源属性=
BindableProperty.Create(
view=>view.ItemsSource,
无效的
BindingMode.TwoWay,
无效的
propertyChanged:(bindableObject、oldValue、newValue)=>
{
((MenuControl)bindableObject).ItemsSourceChanged(bindableObject,oldValue,newValue);
}
);
公共IEnumerable ItemsSource
{
得到
{
返回(IEnumerable)GetValue(ItemsSourceProperty);
}
设置
{
设置值(ItemsSourceProperty,value);
}
}
添加数据触发器
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor={Binding BGColor}>
<Label TextColor="White">
<Label.Triggers>
<!--(or Value ="White" depends on binding value Xamarin.Color or string) -->
<DataTrigger TargetType="Label" Binding={Binding BGColor} Value="#FFFFFF">
<Setter Property="TextColor" Value="Red"/>
<!--(or your color) -->
</DataTrigger>
</Label.Triggers>
</Label>
</StackLayout>
<ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
因此,当您的BGColor(或绑定到颜色的任何属性)属性与您的文本颜色(例如白色)发生冲突时,请使用数据触发器。您可以创建多个,但如果有3个或4个以上,我建议您在这种情况下使用转换器。您可以在xaml中共享ListView的代码。以上代码无法帮助我们找到原因。我已找到解决方案。我将通过api传递颜色,并将其绑定到标签中的textcolor。谢谢大家