ibocon
ibocon

Reputation: 1494

Xamarin.Forms Compiled Bindings does not work on DataTemplate

I am having trouble with Use compiled bindings in a DataTemplate.

I added XamlComilation in App.xaml.cs

// https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xamlc
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Solution.Project

And I changed my DataTemplate to

<DataTemplate x:Key="RectLayerDataTemplate" x:DataType="{x:Type viewmodels:RectLayerViewModel}">
    <forms:RectLayerView forms:ValueX="{Binding ValueX}"
                         forms:ValueY="{Binding ValueY}"
                         forms:ValueWidth="{Binding ValueWidth}"
                         forms:ValueHeight="{Binding ValueHeight}"
                         forms:Color="{Binding Color}" />
</DataTemplate>

However, DataTemplate does not applied to BindableLayout.ItemSource.

<AbsoluteLayout x:Name="CanvasLayout"
                BindableLayout.ItemsSource="{Binding LayerViewModels}" />

Upvotes: 1

Views: 820

Answers (2)

Ben
Ben

Reputation: 2985

Sample usage of compiled bindings in a DataTemplate. Below ItemViewModel is the type of the items in a ListView.ItemsSource:

XAML:

xmlns:vm="clr-namespace:MyViewModels"
...
<DataTemplate x:Key="ListViewItemTemplate" x:DataType="vm:ItemViewModel" >
...

<ListView ... ItemTemplate="{StaticResource ListViewItemTemplate}"/>

ItemViewModel:

namespace MyViewModels
{
  public class ItemViewModel : ViewModelPropertyBase
  ...

  public abstract class ViewModelPropertyBase : INotifyPropertyChanged
  ...

Upvotes: 0

Leo Zhu
Leo Zhu

Reputation: 14956

Did you use AbsoluteLayout to make the layout overlap, so you think it didn't work?

I create a simple sample,with the DataTemplate and set it to the StackLayout BindableLayout,it works well.

the page.xaml:

<StackLayout x:Name="CanvasLayout" Orientation="Vertical"
             BindableLayout.ItemsSource="{Binding LayerViewModels}" />

page.xam.cs:

public MyPage()
    {
        InitializeComponent();
        BindingContext = new MyData();          
        LayerDataTemplate s = new LayerDataTemplate();
        BindableLayout.SetItemTemplate(CanvasLayout, s.template);
    }

MyData :

public class MyData
{

    public ObservableCollection<RectLayerViewModel> LayerViewModels { set; get; } = new ObservableCollection<RectLayerViewModel>();

    public MyData()
    {
        for (var x = 0; x < 6; x++)
        {
            LayerViewModels.Add(new RectLayerViewModel { Name = $"Added at {LayerViewModels.Count}", Type = 1 });
        }
    }
}

RectLayerViewModel:

public class RectLayerViewModel
{
    public string Name { get; set; }
    public int Type { get; set; }
}

DataTemplate:

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary  xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"     
         xmlns:local ="clr-namespace:EntryCa"
         x:Class="EntryCa.LayerDataTemplate">
<DataTemplate x:Key="RectLayerDataTemplate" x:DataType="local:RectLayerViewModel">

         <Label Text="{Binding Name}"
                   TextColor="Red"
                   FontAttributes="Bold" />
  
</DataTemplate>

Upvotes: 3

Related Questions