【问题标题】:Xamarin forms Listview selected Item fore colorXamarin 表单 Listview 选定项目前景色
【发布时间】:2018-08-13 20:34:58
【问题描述】:

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

但是没有示例我能够找到所选项目的前景色的地址。

我会像处理背景一样使用自定义渲染,还是我备份了错误的树?

我的列表视图定义如下

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

感谢任何反馈,谢谢

【问题讨论】:

  • 您需要为此使用自定义渲染器。
  • @Bejasc,还有更多信息吗?已经为背景颜色使用自定义渲染
  • 当你说“前色”时——你想要改变的究竟是什么?文字颜色?未选择时的颜色?什么时候选择颜色?
  • 是的,应该更具体一点。视图单元堆栈布局中标签的文本颜色。
  • 更改标签的文本颜色非常简单,我已经为您添加了如何做到这一点的答案。抱歉,如果我误解了您的意思。

标签: xaml xamarin xamarin.forms


【解决方案1】:

您可以通过向绑定的对象添加另一个属性并将标签上的TextColor 绑定到此新属性来执行此操作(无需自定义渲染器)。

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

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 控件,带有 name 属性和 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>

代码背后

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

我还在BoundObject 中添加了ID 字段,因此我们可以更轻松地识别我们选择了哪个对象。

    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,类型为Color
  • 将您的BoundObject 存储在List&lt;BoundObject&gt;
  • 首次填充您的列表时,在您的BoundObject 中设置TextColor 属性
  • 在列表的ItemSelected 事件中,获取当前选择,并根据您的条件需要更新List&lt;BoundObject&gt; 设置颜色
  • 将列表ItemSource 设置为空,并将其重新分配给(现已更新)List&lt;BoundObject&gt;

【讨论】:

  • 感谢您的努力,但这如何应用于为列表视图中选定单元格的标签设置文本颜色?
  • 啊,对了,所以您只想在这些标签被选中时更改它们的颜色?
  • @Hursey 我已经为你更新了我的答案,更好地理解了你的问题。您确实不需要此特定解决方案的自定义渲染器。
【解决方案2】:

可以通过, 自定义渲染器,但是使用这种方法时,当单元格包含 ContextAction 时,不会应用颜色。

Using Custom Renderer,

From bugzilla

使用跨平台方式(绑定),这种方法将颜色应用于包括 ContextAction 的所有单元格(布局)

Obviously in Xamarin Forms,

实现的可能方法

Stack Overflow discussion

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 2020-06-27
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2020-08-04
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多