locked
Listview datatemplate returns null datacontext - WP8.1 C#

    Question

  • I have structured the classes like this for my Universal app. Currently working in the WP8.1 part.

    The following classes are put in the shared code. (Hoping to use it in Win8.1)

    • FolderItemViewer.xaml. It is the DataTemplate for a ListView in the MainPage.xaml
    • FolderCollection class, which is the collection that is bound to the Listview in the Mainpage.xaml of the WP

    Now the problem is, I have wired the manipulation events to the datatemplate grids in the FolderItemViewer.xaml to capture the right and left swipe and it is working. Now based on this, I need to update that CollectionItem in FolderCollection class and hence the ListView in Mainpage.xaml.

    1. How do I capture that listview item or the collectionitem bound since the manipulation events lie in the FolderItemViewer class?
    2. Can I get the listview item? Or a call back function to the listlivew item template changed? or somethin like that?

    This is the FolderItemViewer.xaml

    <UserControl
    x:Class="JusWrite2.FolderItemViewer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:JusWrite2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid x:Name="MainGrid">
        <Grid Height="60" Width="380" Margin="0,0,0,1">
            <Grid x:Name="ItemGrid" HorizontalAlignment="Left" VerticalAlignment="Center" Width="380" Height="60" Background="Transparent" Canvas.ZIndex="2"
                                          ManipulationMode="TranslateX,System" ManipulationStarted="On_ChannelItem_ManipulationStarted" ManipulationDelta="On_ChannelItem_ManipulationDelta" ManipulationCompleted="OnChannelItemManipulationCompleted">
                <TextBlock x:Name="titleTextBlock" Margin="20,0,0,0" Canvas.ZIndex="2" VerticalAlignment="Center" TextAlignment="Left" FontSize="25" >
                </TextBlock>
            </Grid>
            <Grid x:Name="DelGrid" Opacity="0.0" HorizontalAlignment="Right" VerticalAlignment="Center" Height="60" Background="Red" Canvas.ZIndex="-1" Tapped="On_ChannelDelete_Tap" Width="380">
                <Button Content="X" FontSize="25" Canvas.ZIndex="-1" VerticalAlignment="Center" HorizontalAlignment="Center" Width="380" BorderThickness="0" />
            </Grid>
        </Grid>
    </Grid>
    </UserControl>


    code behind

    private void OnChannelItemManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            Grid ChannelGrid = (Grid)sender;
    
            Grid mGrid = (Grid)(ChannelGrid.Parent);
    
            Grid DeleteGrid = (Grid)((Grid)(ChannelGrid.Parent)).Children[1];
    
            FolderCollection swipedItem = ChannelGrid.DataContext as FolderCollection;// grid has null value for datacontext
    
            double dist = e.Cumulative.Translation.X;
            if (dist < -100) 
            {
               // Swipe left
            }
            else
            {
               // Swipe right
            }
    
        }

    FolderCollection.xaml has two classes in it. FolderItem and FolderCollection

    public class FolderItem : INotifyPropertyChanged
        {
          // variables
    
        public FolderItem()
        {
    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public int CompletionStatus
        {
            //code
        }
    
        public int Priority
        {
          //code
        }
    
        public string FolderText
        {
           //code
        }
    
        public int PenColor
        {
            //code
        }
    
        public string UUID
        {
            //code
        }
    
        public string CreateUUID()
        {
            //code
        }
    }
    
    public class FolderCollection : IEnumerable<Object>
    {
        private ObservableCollection<FolderItem> folderCollection = new ObservableCollection<FolderItem>();
    
        private static readonly FolderCollection instance = new FolderCollection();
    
        public static FolderCollection Instance
        {
            get
            {
                return instance;
            }
    
        }
    
        public IEnumerator<Object> GetEnumerator()
        {
            return folderCollection.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    
        public void Add(FolderItem fItem)
        {
            folderCollection.Add(fItem);
        }
    
    
        public ObservableCollection<FolderItem> FolderCollectionInstance
        {
            get
            {
                return folderCollection;
            }
        }
    }

    And this is the MainPage.xaml where I have data bound.

    // Resources
         <DataTemplate x:Key="StoreFrontTileTemplate">
            <local:FolderItemViewer  />
        </DataTemplate>
    
       <ListView x:Name="FolderListView" ItemsSource="{Binding}" 
                       SelectionMode="None"
                       ItemTemplate="{StaticResource StoreFrontTileTemplate}"
                       ContainerContentChanging="ItemListView_ContainerContentChanging">
    code behind
    //Constructor
        FolderListView.DataContext = fc.FolderCollectionInstance;
        FolderListView.ItemsSource = fc.FolderCollectionInstance;
    
    
        private void ItemListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
        {
            FolderItemViewer iv = args.ItemContainer.ContentTemplateRoot as FolderItemViewer;
    
            if (args.InRecycleQueue == true)
            {
                iv.ClearData();
            }
            else if (args.Phase == 0)
            {
                iv.ShowPlaceholder(args.Item as FolderItem);
    
                // Register for async callback to visualize Title asynchronously
                args.RegisterUpdateCallback(ContainerContentChangingDelegate);
            }
            else if (args.Phase == 1)
            {
                iv.ShowTitle();
                args.RegisterUpdateCallback(ContainerContentChangingDelegate);
            }
            else if (args.Phase == 2)
            {
                //iv.ShowCategory();
                //iv.ShowImage();
            }
    
            // For imporved performance, set Handled to true since app is visualizing the data item
            args.Handled = true;
        }
    
        private TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs> ContainerContentChangingDelegate
        {
            get
            {
                if (_delegate == null)
                {
                    _delegate = new TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs>(ItemListView_ContainerContentChanging);
                }
                return _delegate;
            }
        }
        private TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs> _delegate;
    Sorry to put in so much code. But really appreciate somebody's help in getting this right.


    Alfahisham

    Tuesday, October 21, 2014 10:45 AM

All replies

  • It will be better if you can post a repro project here. It's hard to take these code together and make it work.
    Wednesday, October 22, 2014 8:03 AM