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

ViewModel properties not binding

$
0
0

Hello I have some properties in my viewmodel that I want to bind to a custom control. However, these properties are null once they reach the custom controller. Is there something I am missing?

My ViewModel:

public MyViewModel()
    {
      Tiles = new List<TileInfo>();
      Tiles.Add(new TileInfo() { Id = 1, Title = "Title1" });
      Tiles.Add(new TileInfo() { Id = 2, Title = "Title1" });
      Tiles.Add(new TileInfo() { Id = 3, Title = "Title1" });
      Tiles.Add(new TileInfo() { Id = 4, Title = "Title1" });
      Tiles.Add(new TileInfo() { Id = 5, Title = "Title1" });
      Tiles.Add(new TileInfo() { Id = 6, Title = "Title1" });

      TileColor = Color.Green;
    }

My page containing the control and viewmodel:

<ContentPage.BindingContext>
  <vm:MyViewModel />
</ContentPage.BindingContext>

<ctrl:DynamicTileView Tiles="{Binding Tiles}" TileColor="{Binding TileColor}"/>

And the code for the controller:

public static readonly BindableProperty TilesProperty =
       BindableProperty.Create<DynamicTileView, List<TileInfo>>(c => c.Tiles, new List<TileInfo>());

    public static readonly BindableProperty TileColorProperty =
       BindableProperty.Create<DynamicTileView, Color>(c => c.TileColor, default(Color));

    public List<TileInfo> Tiles
    {
      get
      {
        return (List<TileInfo>)GetValue(TilesProperty);
      }

      set
      {
        SetValue(TilesProperty, value);
      }
    }
    public Color TileColor
    {
      get
      {
        return (Color)GetValue(TileColorProperty);
      }

      set
      {
        SetValue(TileColorProperty, value);
      }
    }

Viewing all articles
Browse latest Browse all 91519

Trending Articles



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