Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Binding to call a method of the binding class from a button.

$
0
0

Hey,

So i have a View that i have bound to a List of Timer objects called Timers (custom class i have made), and in the view i have added a start and remove button. when a user clicks start i want them to be able to call the relevant timer object associated with the button underlying method startTimer(). How can i do this?

View code:

<ListView ItemsSource="{Binding Timers, Mode=TwoWay}" SeparatorVisibility="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                    <StackLayout Padding="10,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
                        <Label Text="{Binding _name, Mode=TwoWay}" YAlign="Center"/>
                        <Label Text="{Binding _startTime, Mode=TwoWay}" YAlign="Center" FontSize="Small"/>
                    </StackLayout>
                    <Button Text="Start" // call timers startTimer method></Button>
                    <Button Text="Remove"></Button>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

Timer Class:

public class Timer
{ 
    public int _startTime { get; set;} 
    public bool _hasStarted{ get; set; } 
    public string _name { get; set; }  

    public Timer (string name, int startTime, bool hasStarted = false)
    {
        _name = name; 
        _startTime = startTime; 
        _hasStarted = hasStarted; 
    }

    public void startTimer(){
        //do something here
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles