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

Can I bind to a command from a ListView's parent when I'm using ViewCells in a separate file?

$
0
0

My app needs to switch between two different ViewCells, so I've been using a DataTemplateSelector for this like so:

using MyOrg.PageModels;
using Xamarin.Forms;

namespace MyOrg.ViewCells
{
    public class ItemDataTemplateSelector : DataTemplateSelector
    {
        public ItemDataTemplateSelector()
        {
            // Retain instances 
            SimpleTemplate = new DataTemplate(typeof(SimpleItemViewCell));
            DetailedTemplate = new DataTemplate(typeof(DetailedItemViewCell));
        }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            return ((MyPageModel)container.BindingContext).SimpleModeEnabled ? SimpleTemplate : DetailedTemplate;
        }

        private readonly DataTemplate SimpleTemplate;
        private readonly DataTemplate DetailedTemplate;
    }
}

The issue now is that when creating these cells in the constructor, it's using the ViewCells from separate files to the actual page:

 <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           x:Class="MyOrg.ViewCells.SimpleItemViewCell">
     <Frame CornerRadius="2.5"
             Margin="5, 2.5"
             Padding="10" >
         <StackLayout Orientation="Horizontal">
             <Label Text="{Binding Number}" 
                    TextColor="{StaticResource MidBlue}" 
                    FontSize="Large" />
             <Label Text="{Binding DetailText}" 
                    TextColor="Black" 
                    FontSize="Large" 
                    LineBreakMode="TailTruncation"/>
         </StackLayout>
     </Frame>
     <ViewCell.ContextActions>
         <MenuItem
                   Command="{Binding Path=ParentBindingContext.DeleteSelectedCommand, Source={x:Reference MyPageName}}"
                   Icon="sharp_delete_white_24.png"/>
     </ViewCell.ContextActions>
 </ViewCell>

MyPageName refers to the name of my page from a content page file. But I don't have access to this from this separate file, so the command doesn't fire.

Is there any way I can pass a reference to the page into each of these ViewCells?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>