【问题标题】:Change TextColor in ListView Xamarin.Forms [duplicate]在 ListView Xamarin.Forms 中更改 TextColor [重复]
【发布时间】:2019-04-14 09:10:54
【问题描述】:

P.S 我在 XAML 中有 ListView 和 TextCell 但我想更改 textColor 在我的代码中

我正在为我的应用程序执行 DarkMode,我有下一个问题: 单元格 - 文本单元格

Cell.TextColor = Color.White; 显示错误:当前上下文中不存在单元格

如何将它绑定到我的上下文或更改文本颜色。请告诉我有什么方法可以做到这一点......

UPD:

创建单元格:

我是用 XAML 实现的

     <StackLayout Margin="20,35,20,20" x:Name="Main_View2">
            <ListView x:Name="Main_Menu" ItemsSource="{Binding Planets}" ItemSelected="Handle_ItemSelected" ItemTapped="Handle_ItemTapped">
            <ListView.ItemTemplate>
            <DataTemplate >
             <TextCell Text="{Binding Name}" x:Name="labelTable" TextColor="#FF851B">

            </TextCell>
            </DataTemplate>
             </ListView.ItemTemplate>
              </ListView>

      </StackLayout >

【问题讨论】:

  • 显示创建Cell的代码。除此之外,您应该在 xaml 中使用带有绑定的 ViewModel。
  • 我升级了描述。看
  • 同时显示您的 ViewModel 和 Planets 属性的项目模型

标签: c# xaml listview xamarin.forms


【解决方案1】:

Way-1 如下给颜色:

<ListView ItemsSource="{Binding Planets}" CachingStrategy="RecycleElement">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Name}" TextColor="White"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Way-2 :在你的模型(行星)中定义一个属性并在那里提供一个颜色,然后将其绑定如下:

<ListView ItemsSource="{Binding Planets}" CachingStrategy="RecycleElement">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Name}" TextColor="{Binding MyTextColor}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Way-3:在 ViewModel 中为 TextColor 定义一个属性,然后像下面这样绑定它

Color myTextColor = Color.White;
public Color MyTextColor
{
    get { return myTextColor; }
    set { SetProperty(ref myTextColor, value); }
}


<ListView x:Name="Main_Menu" ItemsSource="{Binding Planets}" CachingStrategy="RecycleElement">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Name}" TextColor="{Binding Source={x:Reference Main_Menu}, Path=BindingContext.MyTextColor}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

希望对你有帮助。

【讨论】:

  • SetProperty(ref myTextColor, value);显示错误。
  • 这就是我实现属性更改的方式。您需要根据实现属性更改的方式替换该行。检查您定义的其他属性并根据需要进行更改。
【解决方案2】:

我可以用资源做到这一点: `

App.Current.Resources["textColor"] = Color.Black;`

在 XAML 中:

TextColor="{DynamicResource Key=textColor}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2016-11-08
    • 2014-10-26
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    相关资源
    最近更新 更多