如何在xamarin表单上更改列表视图中所选项目的背景颜色?

时间:2016-12-14 11:21:17

标签: listview xamarin.forms background-color selecteditem

我有像这样的ListView

     <StackLayout Spacing="0">
        <ListView x:Name="lvMenu" SeparatorColor="Green">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <StackLayout Padding="10">
                  <Label HorizontalOptions="FillAndExpand" VerticalOptions="Center" Text="{Binding Title}" TextColor="#03a9f4" />
                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
      </StackLayout>

还有像这样的ItemSelected事件处理程序

    lvMenu.ItemSelected += GotoPage;
    private void GotoPage(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem;
        if (item != null)
        {
            //Change bg
            if (item.Title == "Logout")
            {
                LogoutUser();
            }
            else
            {
                NavigationPage nPage = new NavigationPage((Page)Activator.CreateInstance(item.TargetType)) {
                    Title = item.Title
                };
                this.Detail = nPage;
                this.IsPresented = false;
            }
        }
    }
    public class MasterPageItem
    {
        public string Title { get; set; }
        public Type TargetType { get; set; }
    }

我尝试过默认的listview模板,但没有运气。 任何人都可以建议如何更改所选项目的默认背景颜色吗?

1 个答案:

答案 0 :(得分:4)

使用此代码

List.ItemSelected += (sender, e) =>
{
    if (List.SelectedItem != null || e.SelectedItem != null)
    {
        ((ListView)sender).SelectedItem = null;
    }
};