Morse
Morse

Reputation: 9115

How to bind listview item binding to Viewmodel of the page?

I want to bind items in ListView to a property in ViewModel instead of ItemsSource but after trying Binding Source={x:Reference Name=ThisPage} Path=ViewModel.TimerValue it is not working. I am doing something wrong. Not able to identify it

I tried setting :

Text="{Binding Path=TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}', Source={x:Reference Name=ThisPage}}"

ViewModel does implement INotifyPropertyChanged and raises PropertyChanged event

Page header - referernce

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App3.Views.MyPage"
             x:Name="ThisPage">
<ListView x:Name="listView" ItemsSource={Binding Items}>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding Path=TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}', Source={x:Reference Name=ThisPage}}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Code behind

 private MyViewModel ViewModel;
 public MyPage () {
     InitializeComponent ();
     ViewModel = new MyViewModel ();
     this.BindingContext = ViewModel;
 }

Upvotes: 3

Views: 4503

Answers (3)

Abhijith C R
Abhijith C R

Reputation: 1610

Two Steps..

  1. Give a reference name to your parent View with x:Name="viewName" (the view which is bound to the ViewModel)

  2. Do the binding as: "{Binding BindingContext.MenuTapCommand, Source={x:Reference viewName}}"

This works.

Upvotes: -2

Shaur Bin Talib
Shaur Bin Talib

Reputation: 95

Late answer but might help someone. In case you are writing layout in code behind. You can bind the context to source viewmodel context in this fashion. I am binding a button to a command inside viewmodel.

btn.SetBinding(Button.CommandProperty, new Binding("BindingContext.YourCommand", source: YourListView));
btn.SetBinding(Button.CommandParameterProperty, ".");

Upvotes: 1

Morse
Morse

Reputation: 9115

I solved it as below

<Label Text="{Binding TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}'}"
        BindingContext="{Binding Source={x:Reference MyPage}, Path=BindingContext}">

Reason Binding was wrong , BindingContext has to be BindableObject. BindingContext is bindable object which in turn references to ViewModel object, and Label.Text has to be BindableProperty of the bindable object . When I referenced Text={Binding ViewModel.TimerValue it was trying to find bindable property in Mypage however ViewModel is just a public property not Bindable object BindingContext = ViewModel casts it to Bindable object , hence I had to use that way for Source and Text just calls path of that referenced bindingcontext

Thanks for all the suggestions! Really appreciate this community's prompt responsiveness!

Upvotes: 3

Related Questions