question

XamarinNewBie-6038 avatar image
0 Votes"
XamarinNewBie-6038 asked JarvanZhang-MSFT answered

Binding properties of a class in ContentView

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?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

According to the above code, it seems that you didn't encapsulate the related properties in the 'DealsTodayViewModel' class. Please add the get and set methods for the properties as below. Please make sure the properties of the PeriodTotals class are also encapsulated.

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

public class PeriodTotals
{
    public string Period { get; set; }
    ...
}


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.