Reputation: 195
I want to bind my Stacklayout's items using Viewmodel i have already declared all the variables and properties and all within my view model so i am mentioning here my code please tell how i'll do this.
This is my XAML Code:
<controls:OffersListView ImageGIF="outlet" BImage="{Binding BannerUri}"/>
And this is my OffersListView.xaml code
<ffimageloading:SvgCachedImage x:Name="imageFront"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Aspect="AspectFill"
Margin="0"
IsVisible="True"
Source="{Binding ImageGIF}"
LoadingPlaceholder="imageBehind"
Finish="ImageFront_Finish"
DownloadStarted="ImageFront_DownloadStarted"/>
<ffimageloading:SvgCachedImage x:Name="imageBehind"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Aspect="AspectFill"
HeightRequest="205"
WidthRequest="350"
IsVisible="False"
Margin="0"
Source="{Binding BImage}"/>
My ViewModel Code is:
public int BannerId
{
get
{
return HomeItems.BannerId;
}
set
{
HomeItems.BannerId = value;
OnPropertyChanged();
}
}
And here i am binding value in my Home.xamal.cs page. Scrollview contains all items which i am trying to bind using MVVM
private void SetItemSource()
{
m_homeViewModel = new ObservableCollection<HomeViewModel>();
scrollview.BindingContext = m_homeViewModel;
}
Now when i am BImage="{Binding BannerUri}"
going to bind BannerUri using Viewmodel then it is throwing an exception the exception is:
No property, bindable property, or event found for 'BImage', or mismatching type between value and property.
This is my Offerlistview xaml code:
================================================================================
<ffimageloading:SvgCachedImage x:Name="imageFront"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Aspect="AspectFill"
Margin="0"
IsVisible="True"
Source="{Binding ImageGIF}"
LoadingPlaceholder="imageBehind"
Finish="ImageFront_Finish"
DownloadStarted="ImageFront_DownloadStarted"/>
<ffimageloading:SvgCachedImage x:Name="imageBehind"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Aspect="AspectFill"
HeightRequest="205"
WidthRequest="350"
IsVisible="False"
Margin="0"
Source="{Binding BImage}"/>
===============================================================================
This is my Offerlistview.xaml.cs
================================================================================
public ImageSource ImageGIF
{
get { return imageFront.Source; }
set { imageFront.Source = value; }
}
public ImageSource BImage
{
get { return imageBehind.Source; }
set { imageBehind.Source = value; }
}
===============================================================================
I think no one has it's answer
Upvotes: 0
Views: 435
Reputation: 3401
You need to create bindable properties in your custom control code-behind and bind them to your inner views.
OffersListview
code behind:
public partial class OffersListview
{
public static readonly BindableProperty ImageGifSourceProperty = BindableProperty.Create(
nameof(ImageGifSource),
typeof(string),
typeof(OffersListview));
public static readonly BindableProperty BannerSourceProperty = BindableProperty.Create(
nameof(BannerSource),
typeof(string),
typeof(OffersListview));
public string ImageGifSource
{
get => (string)GetValue(ImageGifSourceProperty);
set => SetValue(ImageGifSourceProperty, value);
}
public string BannerSource
{
get => (string)GetValue(BannerSourceProperty);
set => SetValue(BannerSourceProperty, value);
}
}
OffersListView
xaml:
<controls:BaseClass x:Class="XXX.XXX.OffersListview"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls:="clr-namespace:XXX.XXX"
x:Name="RootLayout">
...
<ffimageloading:SvgCachedImage HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Aspect="AspectFill"
Margin="0"
IsVisible="True"
LoadingPlaceholder="imageBehind"
Finish="ImageFront_Finish"
DownloadStarted="ImageFront_DownloadStarted"
Source="{Binding Source={x:Reference RootLayout}, Path=ImageGifSource}" />
<ffimageloading:SvgCachedImage HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Aspect="AspectFill"
HeightRequest="205"
WidthRequest="350"
IsVisible="False"
Margin="0"
Source="{Binding Source={x:Reference RootLayout}, Path=BannerSource}" />
...
Upvotes: 1