简体   繁体   中英

Changing a ViewCell element on click

I have built a List<> of item objects, which are being used as the ItemsSource to a ListView . on my ListView's ItemSelected event, I am trying to change one of the elements of that particular item. The original values are bound from the object.

Please consider the following example:

itemClass.cs

class itemClass
{
    public itemClass()
    {
    }
    public string valueIWantToChange {get; set;}
}

MyPage.cs

public MyPage()
{
    InitializeComponent();
    List<itemClass> listOfItems= new List<itemClass>();
    itemClass item1= new itemClass { valueIWantToChange = "UnClicked"};
    listOfItems.Add(item1);
    BindingContext = this;
    lstView.ItemsSource = listOfItems;

    lstView.ItemSelected += (sender, e) =>
    {
        if (e.SelectedItem == null)
        {
            return;
        }

       // The below does nothing but highlights what I am trying to achieve
       ((itemClass)((ListView)sender).SelectedItem).valueIWantToChange = "Clicked" ;

       ((ListView)sender).SelectedItem = null;
   };
}

My XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:myProject="clr-namespace:myProject"
         x:Class="myProject.MyPage">

    <StackLayout>
        <ListView x:Name="lstView">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <StackLayout Padding="0" Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                    <Label Text="{Binding valueIWantToChange }" TextColor="Black" FontSize="16" VerticalOptions="Center" HorizontalOptions="Center" Margin="5,5,5,5"/>
                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

</ContentPage>

I have attempted to use Binding commands and PropertyChangedEventHandler but with no success. Any assistance in this would be greatly appreciated.

Edit

I have attempted to use PropertyChangedEventHandlers in a similar way, elsewhere in the app, please see below:

    public double FontXLarge
    {
        set
        {
            someFontsize = value;
            OnPropertyChanged("FontXLarge");
        }
        get
        {
            someFontsize = scalableFont(10);
            return someFontsize;
        }
    }        

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

The above allows me to bind a fontsize to that scaling font, however if I were to use something similar for string, would it not effect all of the items in the ListView?

I think the best way to implement what you want, it's to use a ViewModel with INotifyPropertyChanged . You can find a lot of example if you're googling a bit. One good link for generic baseclass is this .

Let me know if you find everything you need.

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