Xaml Xamarin表单列表查看选定项前颜色
有点被卡住了。 有一个列表视图,我想更改主题以匹配我的应用程序的其余部分。 下面是几个关于如何更改所选项目背景颜色的示例,我使用自定义渲染非常有效,主要是这个示例 然而,没有一个例子,我能够找到地址的前颜色的选定项目 这是我会像使用背景一样使用自定义渲染还是我备份了错误的树 我的列表视图定义如下Xaml Xamarin表单列表查看选定项前颜色,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,有点被卡住了。 有一个列表视图,我想更改主题以匹配我的应用程序的其余部分。 下面是几个关于如何更改所选项目背景颜色的示例,我使用自定义渲染非常有效,主要是这个示例 然而,没有一个例子,我能够找到地址的前颜色的选定项目 这是我会像使用背景一样使用自定义渲染还是我备份了错误的树 我的列表视图定义如下 <ListView.ItemTemplate> <DataTemplate> <customControls:ExtendedViewCell S
<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>
感谢您的反馈。您可以通过向绑定到的对象添加另一个属性,并将标签上的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>
代码背后的关键点是
- 绑定对象上的新属性
,类型为TextColor
Color
- 将您的
存储在边界对象
列表中
- 第一次填充列表时,请在
中设置BoundObject
属性TextColor
- 在列表的
事件中,获取当前选择,并根据您的条件更新ItemSelected
设置颜色列表
- 将列表
设置为null,并将其重新分配给(现在更新的)ItemSource
列表
- 可以通过,
自定义渲染器,但使用此方法,当单元格包含ContextAction时,不会应用颜色
,
使用跨平台方式(绑定),这种方法将颜色应用于包含ContextAction的所有单元格(布局)
,
获得成功的可能途径
您需要为此使用自定义渲染器。@Bejasc,是否有更多信息?当你说“前颜色”时,已经在为后颜色使用自定义渲染了-你到底想改变什么?文本颜色?未选择时的颜色?什么时候选择颜色?是的,应该更具体一点。视图单元格堆栈布局中标签的文本颜色。更改标签的文本颜色非常简单,我已经为您添加了一个关于如何做到这一点的答案。如果我误解了您的意思,请道歉。非常感谢您的努力,但这如何适用于在列表视图中为选定单元格的标签设置文本颜色?啊,对了,所以您只希望在选定标签时更改这些标签的颜色?@Hursey我已为您更新了我的答案,以便更好地理解您的问题。此特定解决方案不需要自定义渲染器。
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;
}