I have a label inside a listview. I want to change font size of that label on click of a button.
You could set binding to the FontSize property. Detect the button's click event, then traverse the data collection to specify a value to FontSize.
Check the code:
<ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout> <Label Text="{Binding Content}" FontSize="{Binding Size}"/> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate>
Page.xaml.cs
public partial class Page1 : ContentPage { ObservableCollection<Model_Class> models = new ObservableCollection<Model_Class>(); public Page1() { InitializeComponent(); //add the data listview.ItemsSource = models; } private void Button_Clicked(object sender, EventArgs e) { foreach (var item in models) { item.Size = 24; } } }
Model class
public class Model_Class : INotifyPropertyChanged { private int size; public int Size { get { return size; } set { if (size != value) { size = value; NotifyPropertyChanged(); } } } ... protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }
Answers
You could set binding to the FontSize property. Detect the button's click event, then traverse the data collection to specify a value to FontSize.
Check the code:
Page.xaml.cs
Model class