简体   繁体   中英

Highlight the list-view selected item programatically in xamarin.forms

I'm trying to develop a wpf chat application.I've been looking everywhere for a way to select an item in a ListView and change the color of the row to let the user know which row is selected programatically.

What my requirement exactly is:

On login to the application the user is able to see list of channels and contacts and the logged-in user gets a notification when a user sends him a message.If the user clicked on that notification then the message sender contact should be highlighted to let the user know which chat is opened.

I've tried to set the selected item of listview as the sender based on id but still no use.

var index = _myViewModel.Users.IndexOf(selectedUser);

MyListView.SelectedItem = _myViewModel.Users[index];

I've googled about this and found some links which provide solutions for same case in xamarin/xamarin.android/winforms. but nothing solves my problem as the properties they've used are not available in xamarin.forms listview.

Here are the links:

Xamarin Android ListView select item and change row color

How to select an item in a ListView programmatically? .

Is it possible to do like this.If yes,Is there any workaround to achieve this.Can anyone please help me with this.

Thanks.

You can refer my answer given in one of the previous post: ListView not unselecting

You can create Custom view cell of your List view and add background color to selected view cell.

Hope this may solve your issue.

Solution:

You can bind your Cell's background color with the property in your model. When the user clicked on that notification, change the background color of the specific cell in model and then it will change in listview:

    private void ClickNotification(object sender, EventArgs e)
    {
        var index = _myViewModel.Users.IndexOf(selectedUser);

        //change the background color of the specific cell in model
        myModel model = _myModelList[index];
        model.myBackColor = Color.Blue;
    }

In the model:

class myModel : INotifyPropertyChanged
{
    Color backColor;

    public event PropertyChangedEventHandler PropertyChanged;

    public myModel( Color myColor)
    {
        backColor = myColor;
    }

    public Color myBackColor
    {
        set
        {
            if (backColor != value)
            {
                backColor = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("myBackColor"));
                }
            }
        }
        get
        {
            return backColor;
        }
    }
}

And in the xaml:

<ListView x:Name="myListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal" BackgroundColor="{Binding myBackColor}">

                </StackLayout>

            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM