My problem is with the viewcell, the OnDelete command is not found due to it being of the IssueModel class, I've attempted to change the binding-context of the Listview, but that doesn't change anything except the above binding.
Is there any way to change the binding context of the viewcell so I don't have to put the command into to IssueModel?
freshMvvm:FreshBaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:ASFT.Converters;assembly=ASFT"
xmlns:freshMvvm="clr-namespace:FreshMvvm;assembly=FreshMvvm"
xmlns:helperMethods="clr-namespace:ASFT.HelperMethods;assembly=ASFT"
x:Class="ASFT.Pages.IssueListPage">
<ContentPage.Resources>
<ResourceDictionary>
<converters:SelectedItemEventArgsToSelectedItemConverter x:Key="SelectedItemConverter" />
<converters:DateTextConverter x:Key="DateToTextConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ListView ItemsSource="{Binding Issues}" SeparatorColor="#444444" RowHeight="90" IsPullToRefreshEnabled="True" IsRefreshing="{Binding IsBusy}" RefreshCommand="{Binding PullRefreshCommand}" >
<ListView.Behaviors>
<helperMethods:EventToCommandBehavior EventName="ItemSelected"
Command="{Binding OnSelectedIssueCommand}"
Converter="{StaticResource SelectedItemConverter}" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<ViewCell.ContextActions>
<MenuItem Command="{Binding OnDelete}" Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Source="{Binding SeverityImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="70"/>
<Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="2" Source="{Binding StatusImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="60"/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Title}" LineBreakMode="TailTruncation" YAlign="Center" VerticalOptions="Start" Font="Bold, Medium"/>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding Created, Converter={StaticResource DateToTextConverter}}" YAlign="Center" VerticalOptions="Start" Font="Medium"/>
<Label Grid.Row="2" Grid.Column="1" Text="{Binding Description}" LineBreakMode="WordWrap" YAlign="Start" VerticalOptions="Start" Font="Small"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</freshMvvm:FreshBaseContentPage>
Edit:
I've attempted one of the answers but that didn't work. This just gets an error message: expected type is object, but type is IssueListPageModel
xmlns:pageModels="clr-namespace:ASFT.PageModels;assembly=ASFT"
<MenuItem Command="{Binding Path=BindingContext.OnDelete, Source={pageModels:IssueListPageModel}}" Text="Delete" IsDestructive="True" />
Add a x:Name
attribute to your freshMvvm:FreshBaseContentPage
, like i.e.: x:Name="MyAwesomePage"
.
Now change your ViewCell
binding like this:
<MenuItem Command="{Binding Path=BindingContext.OnDelete, Source={x:Reference Name=MyAwesomePage}}" Text="Delete" IsDestructive="True" />
Now the binding source is set to the page by using its name. And the path is set to the property BindingContext.OnDelete
. So, in your backing view model for this page there should be a OnDelete
property.
To clarify the separate components like you've asked in the comments.
The Path=
is omitted with a regular binding. When not explicitly mentioned, {Binding MyProperty}
means the same as '{Binding Path=MyProperty}'. Path
means the path to the value that needs to be bound from the BindingContext
, so effectively the property that you are binding to.
The Source
is used to specify what is the source of the Path
. Which is in itself another binding. In our case to a reference which is known by the name we just gave the page. This way, the binding of the ViewCell
knows to start at the Source
and then search for the Path
to retrieve its value. I hope this makes it a bit clear.
You can reference anything here if you want to, as long as you have access to the instance of the class here. I would, however, recommend to keep it to the BindingContext
which is effectively the view model (note: BindingContext
is the actual property of your page that contains the view model). Else you will lose overview very quickly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With