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

Using a DataTemplate in a UserControl

$
0
0

Hi Developers,

I created a UserControl that shows Tiles from a List (ItemSource) similar to the ListView control.

My Tiles user Control is currently inheriting from a ContentView and I want it to behave like a ListView, so is it possible to use a DataTemplate to customize the Tiles?

This is the method that creates the tiles but this is very static:

        private void UpdateTiles()
        { 
            foreach (TileGridModel item in ItemsSource)
            {
                var tileItem = new Frame
                {
                    Margin = 10,
                    CornerRadius = 10,
                    HasShadow = false,                    
                    WidthRequest = widthHeight,
                    HeightRequest = widthHeight,
                    OutlineColor = Color.Blue,
                    BackgroundColor = Color.LightGray,
                    Content = new StackLayout
                    {
                        //Padding = 5,                        
                        Orientation = StackOrientation.Vertical,
                        Children =
                        {
                            new CircleImage { Source = item.ImageSource, WidthRequest = widthHeight, HeightRequest = widthHeight / 2, BorderThickness = 2, BorderColor = (Color) Application.Current.Resources["ColorAccent"] },
                            new Label { Text = item.Title, HorizontalTextAlignment = TextAlignment.Center }
                        }
                    }
                };

                var recognizer = new TapGestureRecognizer();
                recognizer.Tapped += OnTileItemTapped;

                tileItem.GestureRecognizers.Add(recognizer);

                mainWrapLayout.Children.Add(tileItem);
            }
        }

So it whould be very nice if I could use a DataTemplate to create the look of the tiles in XAML.
Any ideas how I can realize it?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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