xaml - Xamarin 表单 Listview 选定的项目前景色

标签 xaml xamarin xamarin.forms

有点卡在这个上了。 有一个 ListView ,我想更改主题以匹配我的应用程序的其余部分。 一直在关注如何更改所选项目背景颜色的几个示例,我使用自定义渲染效果非常好,主要是这个示例 https://blog.wislon.io/posts/2017/04/11/xamforms-listview-selected-colour

但是,我找不到任何示例来解决所选项目的前景色。

这是我对自定义渲染做的事情,就像背景一样,还是我备份了错误的树?

我的 ListView 定义如下

<ListView.ItemTemplate>
    <DataTemplate>
        <customControls:ExtendedViewCell SelectedBackgroundColor="#5DB8B3">
            <ViewCell.View>
                <StackLayout VerticalOptions="StartAndExpand">
                    <Label Text="{Binding AttributeName}" 
                           FontSize="Small" 
                           FontAttributes="Bold"/>
                    <Label Text="{Binding Description}" 
                           FontSize="Small"/>
                    <Label Text="{Binding CreditorName}" 
                           FontSize="Small"/>
                </StackLayout>
            </ViewCell.View>
        </customControls:ExtendedViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

感谢任何反馈,谢谢

最佳答案

您可以通过向绑定(bind)的对象添加另一个属性并绑定(bind) TextColor 来做到这一点(无需自定义渲染器)在这个新属性的标签上。

假设你的绑定(bind)对象看起来像这样

public class BoundObject
{
    public string AttributeName { get; set; }
    public string Description { get; set; }
    public string CreditorName { get; set; }

    public int id { get; set; }
    public Color TextColor { get; set; }
}

XAML

请注意添加的 ListView 控件,其中包含名称属性和 ItemSelected 事件。

<ListView x:Name="myList" ItemSelected="myListSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout VerticalOptions="StartAndExpand">
                    <Label Text="{Binding AttributeName}" 
                        FontSize="Small" 
                        FontAttributes="Bold"
                        TextColor="{Binding TextColor}"
                    />

                    <Label Text="{Binding Description}" 
                        FontSize="Small"
                        TextColor="{Binding TextColor}"
                    />

                    <Label Text="{Binding CreditorName}" 
                        FontSize="Small"
                        TextColor="{Binding TextColor}"
                    />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

代码隐藏

大部分魔法都发生在后面的代码中。请注意,我只是将一些项目添加到此处开始的列表中 - 仅用于调试目的。需要注意的是,起始颜色也在需要创建列表时给出。

我还添加了 ID字段到 BoundObject ,这样我们就可以更轻松地识别我们选择了哪个对象。

    List<BoundObject> listItems = new List<BoundObject>();

    public YourPage()
    {
        InitializeComponent();

        for (int i = 0; i < 10; i++)
        {
            listItems.Add(new BoundObject() { id=i, AttributeName = "Attribute " + i, Description = i + " description", CreditorName = "Creditor: " + i, TextColor = Color.Blue });
        }

        myList.ItemsSource = listItems;
    }


    private void myListSelected(object sender, SelectedItemChangedEventArgs e)
    {
        if (((ListView)sender).SelectedItem == null)
            return;

        //Get the item we have tapped on in the list. Because our ItemsSource is bound to a list of BoundObject, this is possible.
        var selection = (BoundObject)e.SelectedItem;

        //Loop through our List<BoundObject> - if the item is our selected item (checking on ID) - change the color. Else - set it back to blue 
        foreach(var item in listItems)
        {
            if (item.id == selection.id)
                item.TextColor = Color.Red;
            else
                item.TextColor = Color.Blue;
        }

        //ItemsSource must be set to null before it is re-assigned, otherwise it will not re-generate with the updated values. 
        myList.ItemsSource = null;
        myList.ItemsSource = listItems;
    }

隐藏代码的关键点是......

  • 新特性 TextColor在您绑定(bind)的对象上,类型为 Color
  • 存储您的BoundObjectList<BoundObject>
  • 首次填充列表时,请设置 TextColor您的属性(property)BoundObject
  • ItemSelected列表中的事件,获取当前选择,然后更新List<BoundObject>根据您的情况需要设置颜色
  • 设置列表 ItemSource为 null,并将其重新分配给(现已更新)List<BoundObject>

关于xaml - Xamarin 表单 Listview 选定的项目前景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49123155/

相关文章:

c# - C# 中用于系统键的 KeyDown 事件

wpf - 从默认样式继承样式

android - Visual Studio 2015 Xamarin.Forms PCL Android 调试 - 空白异常,无调用堆栈,无本地变量

xamarin.forms - 如果 Grid.Row 已知,如何获取网格行的子级

c# - 我想每分钟运行一个从我的 OnAppearing() 开始的方法。我需要将其作为任务运行吗?

c# - 删除选择器 XAMARIN 中的行

c# - WPF Windows 8 兼容性问题

Xamarin 复制文件跨平台

c# - 以编程方式创建按钮单点触控

c# - 代码无法识别 xaml 中的名称 View (Xamarin Forms Visual Studio 2015)