question

JesseKnott-2239 avatar image
0 Votes"
JesseKnott-2239 asked JesseKnott-2239 commented

Can I nest data templates within eachother?

Hello, in my sample code here I am using a set of Syncfusion controls, but this should apply to any controls or templates.

I want to make a ComboBox, that is comprised of another set of controls, in this case Chip controls.
Here is my sample code.

                 Supplies.DataSource = viewModel.SuppliesList;
                 Supplies.ItemTemplate = new DataTemplate(() =>
                 {
                     SfChip chip = new SfChip();
                     chip.ControlTemplate = new ControlTemplate(() =>
                     {
                         StackLayout sl = new StackLayout() { Orientation = StackOrientation.Horizontal };
                         var optionIcon = new Image { HeightRequest = 20, WidthRequest = 20, HorizontalOptions = LayoutOptions.Start, };
                         optionIcon.SetBinding(Image.SourceProperty, "Image");
                         var displayName = new Label { VerticalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.Start, Style = (Style)Application.Current.Resources["DataLabel"] };
                         displayName.SetBinding(Label.TextProperty, "Name");
    
                         sl.Children.Add(optionIcon);
                         sl.Children.Add(displayName);
    
                         return sl;
                     });
    
                     return chip;
                 });

Is there some other/better way to do this? or is this not possible? (again speaking outside of any proprietary constraints. AKA is this just a syncfusion limitation and the nest should work?)

Thanks much!
Cheers!

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

For some reason I can't add this code, I forgot to mention this in the above code.
This is the code that populates the data for the control.


     /// <summary>
     /// Populate the supply options for the retailers.
     /// </summary>
     private void BuildSupplyOptions()
     {
         SuppliesList = new ObservableCollection<RetailerSuppiles>
         {
             new RetailerSuppiles { Name = "Guns", Image = SniperRifleIcon },
             new RetailerSuppiles { Name = "Ammo", Image = BulletIcon },
             new RetailerSuppiles { Name = "Archery", Image = ArcheryIcon },
             new RetailerSuppiles { Name = "Reloading", Image = PowderScalesIcon },
             new RetailerSuppiles { Name = "Gear", Image = SwissArmyIcon },
             new RetailerSuppiles { Name = "Other", Image = AccessoryIcon },
         };
     }
0 Votes 0 ·

0 Answers