Sooo, im building a Prism, xamarin.forms app.
I have a object by the name of "Herd", with basic attributes.
Im displaying a list of these objects, and want to activate a command that sends the herd that is checked in a checkbox to the viewmodel.
<ListView
x:Name="UpdateHerdList"
ItemsSource="{Binding HerdsThatsNeedsToBeUpdated}"
RowHeight="60"
>
<ListView.ItemTemplate>
<DataTemplate>
<CustomRenderer:TransparentViewCell>
<Grid
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Label
TextColor="Black"
FontSize="15"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start"
Grid.Column="0"
Text="{Binding HeaderName}"
/>
<CheckBox
Grid.Column="1"
HorizontalOptions="Center"
ScaleX="1.75"
ScaleY="1.75"
Color="Black"
IsChecked="True"
>
<CheckBox.Behaviors>
<b:EventToCommandBehavior
EventName="PropertyChanged"
Command="{Binding UpdateThisHerdCommand}"
CommandParameter="{Binding Herd}"
/>
</CheckBox.Behaviors>
</CheckBox>
</Grid>
</CustomRenderer:TransparentViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
But i cant seem to find the right way to bind to the viewmodel inside the itemsource of the listview.
Can someone please help guiding me to the right binding.
Thanks in advance!
About binding one command to checkbox, I do one sample that you can see the following code. If you change checkbox checked, will pass current listview item.
<ListView
x:Name="UpdateHerdList"
ItemsSource="{Binding HerdsThatsNeedsToBeUpdated}"
RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
FontSize="15"
HorizontalTextAlignment="Start"
Text="{Binding HeaderName}"
TextColor="Black"
VerticalTextAlignment="Center" />
<CheckBox
Grid.Column="1"
HorizontalOptions="Center"
IsChecked="True"
ScaleX="1.75"
ScaleY="1.75"
Color="Black">
<CheckBox.Behaviors>
<b:EventToCommandBehavior
Command="{Binding BindingContext.UpdateThisHerdCommand, Source={x:Reference UpdateHerdList}}"
CommandParameter="{Binding .}"
EventName="CheckedChanged" />
</CheckBox.Behaviors>
</CheckBox>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public partial class Page34 : ContentPage
{
public ObservableCollection<Herd> HerdsThatsNeedsToBeUpdated { get; set; }
public ICommand UpdateThisHerdCommand { get; set; }
public Page34()
{
InitializeComponent();
HerdsThatsNeedsToBeUpdated = new ObservableCollection<Herd>()
{
new Herd(){HeaderName="test 1"},
new Herd(){HeaderName="test 2"},
new Herd(){HeaderName="test 3"},
new Herd(){HeaderName="test 4"},
new Herd(){HeaderName="test 5"},
new Herd(){HeaderName="test 6"}
};
UpdateThisHerdCommand = new Command<Herd>(checkboxcommand);
this.BindingContext = this;
}
private void checkboxcommand(Herd herd)
{
Console.WriteLine("the selected item is {0}",herd.HeaderName);
}
}
public class Herd
{
public string HeaderName { get; set; }
}
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.