【问题标题】:how to change custom render label textcolor inside list view in xamarin forms如何在 xamarin 表单的列表视图中更改自定义渲染标签 textcolor
【发布时间】:2020-10-20 00:38:24
【问题描述】:

我们正在开发一个小型应用程序,我们使用自定义渲染创建了仪表板,但我无法更改标签文本颜色。它默认显示标签文本颜色为白色,列表视图背景颜色它将通过 api 出现,因此如果它是白色背景,则标签文本颜色无法看到。在这里,我附上了下面的代码。给我建议来解决这个问题

菜单控件自定义渲染

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);

            }
        }

【问题讨论】:

  • 您可以在 xaml 中分享 ListView 的代码。上面的代码无法帮助我们找出原因。

标签: xamarin.forms visual-studio-2019


【解决方案1】:

添加数据触发器

<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 个,我建议您在这种情况下使用转换器。

【讨论】:

  • 我找到了解决方案。我将通过 api 传递颜色并将其绑定到标签中的 textcolor。谢谢大家
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多