Dav*_*het 2 xaml listview xamarin xamarin.forms
我有一个ListView
, 我需要将它的原生colors
(所选项目和其他项目)替换为不同的颜色。我无法找到如何做到这一点。我可以将背景颜色更改为不同的颜色(请参阅下面的代码),但我不知道如何使其表现得像普通的ListView
、在选择时更改项目颜色。
这是我的代码:
<ListView x:Name="MenuItemsListView"
SeparatorVisibility="None"
HasUnevenRows="true"
ItemsSource="{Binding MenuItems}">
<ListView.Header>
<Grid BackgroundColor="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Image Grid.Column="1" Grid.Row="1" WidthRequest="50" HeightRequest="50" HorizontalOptions="StartAndExpand" Source="Assets\logo.png" />
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="100">
<StackLayout Padding="15,10"
Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
BackgroundColor="{StaticResource LogoBackgroundColor}">
<Image WidthRequest="50" Source="{Binding IconSource}" />
<Label VerticalOptions="FillAndExpand"
VerticalTextAlignment="Center"
Text="{Binding Title}"
FontSize="24"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
这也可以Triggers
在 xaml 中使用,但这也行得通。
要更改color
selected ViewCell
,有一个简单的解决方法,无需使用自定义渲染器。使Tapped
您的事件ViewCell
如下
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Tapped="ViewCell_Tapped">
<StackLayout></StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
在您ContentPage
的 cs 文件中,实现事件
private void ViewCell_Tapped(object sender, System.EventArgs e)
{
if(lastCell!=null)
lastCell.View.BackgroundColor = Color.Transparent;
var viewCell = (ViewCell)sender;
if (viewCell.View != null)
{
viewCell.View.BackgroundColor = Color.Red;
lastCell = viewCell;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样lastCell
在你的顶部声明ContentPage
ViewCell lastCell;
输出截图:
归档时间: |
|
查看次数: |
4851 次 |
最近记录: |