0

I'm trying to achieve what I think is probably quite simple but as I'm new to Xamarin & Databinding I think I'm getting in a spin. I have a very simple ContentPage that just has a Databinding to my viewModel for this page and my ContentView, TotalsTemplate.

<ContentPage.BindingContext>
    <vm:DealsTodayViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout>
        <template:TotalsTemplate></template:TotalsTemplate>
    </StackLayout>
</ContentPage.Content>

My viewmodel has a public property of my class, Totals, which has basic int,string,decimal props.

    public class DealsTodayViewModel
    {
        Public string ViewModelPeriod;
        public PeriodTotals Totals;
        public DealsTodayViewModel()
        {
            ViewModelPeriod = "TODAY";
            Totals = new PeriodTotals
            {
                Period = "DAILY",
                ClientServices_Deals_Chicago = 1,
                ClientServices_Deals_Manchester_Na = 1,
                ClientServices_Deals_Manchester_Uk = 1,
                ClientServices_Ramp_Chicago = 1.2m,
                ClientServices_Ramp_Manchester_Na = 1.3m,
                ClientServices_Ramp_Manchester_Uk = 1.4m
        };
    }
}

Now in my TotalsTemplte ContentView I have a Grid with following inside.

    <Label Text="{***Binding ViewModelPeriod***}" FontAttributes="Bold"/>
    <Frame OutlineColor="Black" Grid.Row="0" Grid.Column="1">
        <Label Text="{Binding ***Totals.Period***}" FontAttributes="Bold"/>
    </Frame>

My String property on the DealsTodayViewModel is visible in my ContentView but not the Perod property from inside my Totals property, am I binding incorrectly to this?

Cfun
  • 8,442
  • 4
  • 30
  • 62
PAblo
  • 39
  • 5
  • you an only bind to **public properties**. `Totals` is not a C# property – Jason Jan 23 '21 at 11:00
  • Totals is a public property of type PeriodTotals, my underlying class Jason? – PAblo Jan 23 '21 at 11:02
  • please lookup the definition of a C# property – Jason Jan 23 '21 at 11:07
  • What is Totals then? – PAblo Jan 23 '21 at 11:11
  • https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property – Jason Jan 23 '21 at 11:12
  • Please avoid putting tags in title when it is not necessary [Should I use tags in titles?](https://stackoverflow.com/help/tagging) – Cfun Jan 23 '21 at 11:16
  • So this isn't an automatically-implemented property then? – PAblo Jan 23 '21 at 11:17
  • Does this answer your question? [Why does WPF support binding to properties of an object, but not fields?](https://stackoverflow.com/questions/842575/why-does-wpf-support-binding-to-properties-of-an-object-but-not-fields) – Cfun Jan 23 '21 at 11:18
  • 1
    no, it is a field. A property will have a get/set – Jason Jan 23 '21 at 11:19
  • Cheers both, really helps me out. – PAblo Jan 23 '21 at 11:23
  • I summarized your comments and add an answer there. Can you please accept it (click the ☑️ in the upper left corner of this answer ) so that we can help more people with same problem:). – nevermore Jan 25 '21 at 06:13

1 Answers1

0

From the document, data-binding should binding between properties instead of field:

Data binding is the technique of linking properties of two objects so that changes in one property are automatically reflected in the other property. Data binding is an integral part of the Model-View-ViewModel (MVVM) application architecture.

So the solution is change the fields in your vm to properties:

public class DealsTodayViewModel
{
    public string ViewModelPeriod { get; set; }
    public PeriodTotals Totals { get; set; }
    public DealsTodayViewModel()
    {
        ...
    }
}

Refer: field and property

nevermore
  • 15,432
  • 1
  • 12
  • 30