How do I use XAML to bind to a property inside a class instance in the view-model?

EasyGoingPatEasyGoingPat GBMember ✭✭✭

I am struggling to get a binding working. In my code, I have something like this:

public class SomeClass
{
    public string AProperty { get; set; } = "ABC";
}
public class MyViewModel
{
    public SomeClass { get; set; } = new SomeClass();
}

I am trying to bind a Label to AProperty. MyViewModel is set as the DataContext for the window. My XAML is as follows:

<Label Text="{Binding Source={x:Reference Name=SomeClass},Path=AProperty}"/>

This gives a System.Exception: Can't resolve name on Element.

Can anyone spot what I am doing wrong?

Best Answers

  • NMackayNMackay GB mod
    edited July 2016 Accepted Answer

    Here's a sample.

    <local:CustomTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                            xmlns:mvvm="clr-namespace:OrderFinder;assembly=OrderFinder"
                            BindingContext="{Binding Source={x:Static mvvm:App.Locator}, Path=Details}"
                            Title="{Binding SelectedOrder.OrderName}"
    
            private OrderSearchResponse _selectedOrder;
            public OrderSearchResponse SelectedOrder
            {
                get { return _selectedOrder; }
                set { Set(() => SelectedOrder, ref _selectedOrder, value); }
            }
    

Answers

  • NMackayNMackay GBInsider, University mod
    edited July 2016 Accepted Answer

    Here's a sample.

    <local:CustomTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                            xmlns:mvvm="clr-namespace:OrderFinder;assembly=OrderFinder"
                            BindingContext="{Binding Source={x:Static mvvm:App.Locator}, Path=Details}"
                            Title="{Binding SelectedOrder.OrderName}"
    
            private OrderSearchResponse _selectedOrder;
            public OrderSearchResponse SelectedOrder
            {
                get { return _selectedOrder; }
                set { Set(() => SelectedOrder, ref _selectedOrder, value); }
            }
    
  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @NMackay You, sir, are a scholar and a gent. I've been reading and reading about data binding for months and - notwithstanding my faulty memory - I am pretty sure I have never come across an example that shows you can simply use the dot operator. Brilliant! That has solved so many problems for me.

    (Sorry about the confusion on the class naming. Definitely could have made that clearer.)

    Kind wishes ~ Patrick

  • NMackayNMackay GBInsider, University mod

    Your welcome :smile:

  • davsdavs ECMember

    Hi,

    Please can someone tell me how to extract the Text property of the Label control in the rating example?

    This is the post:
    https://blog.xamarin.com/behaviors-in-xamarin-forms/

    This is the xaml page:
    https://github.com/nishanil/Xamarin.Forms-Samples/blob/master/Samples%20(1.3)/Samples/Samples/DataEntryPage.xaml

    This is the xaml where is the label with the property text binding to a Rating property with an converter "ratingConverter":
    <StackLayout> <StackLayout.Resources> <ResourceDictionary> <local:RatingConverter x:Key="ratingConverter" /> <Style TargetType="Label" BasedOn="{StaticResource baseStyle}"> <Setter Property="TextColor" Value="#4CAF50" /> </Style> </ResourceDictionary> </StackLayout.Resources> <Label Text="{Binding Source={x:Reference starFive}, Path=Rating, Converter={StaticResource ratingConverter}}" ></Label> </StackLayout>

    I want to extract the selected rating in the code of the viewmodel, or how can I link this value to a property in the viewmodel?

    Thanks in advance

  • XamZXamZ INMember ✭✭
    edited June 2017

    ViewModel

        ICommand submitCommand;
        public DataEntryViewModel()
        {
            // configure the SubmitCommand with a method
            submitCommand= new Command(OnTapped);
        }
        public ICommand SubmitCommand 
        {
            get { return submitCommand; }
        }
        void OnTapped(object s)
        {
            var result = s;
        }
    
  • XamZXamZ INMember ✭✭
    edited June 2017

    In View

              Command="{Binding SubmitCommand, Mode=TwoWay}"
              CommandParameter="{Binding Source={x:Reference starFive},Path=Rating}" 
    
  • sholikhinsholikhin Member ✭✭

    @NMackay said:
    @EasyGoingPat

    Can you not just bind directly to the object and it's property?

    <Label Text="{Binding SomeClass.AProperty}" />

    Bit confusing as you made your property get and class name the same :smile:

    In case of the same name, it right that we should bind to the instance name those class ?. I mean not binding to the name / definition of the class ?.

  • sholikhinsholikhin Member ✭✭

    @sholikhin said:

    @NMackay said:
    @EasyGoingPat

    Can you not just bind directly to the object and it's property?

    <Label Text="{Binding SomeClass.AProperty}" />

    Bit confusing as you made your property get and class name the same :smile:

    In case of the same name, it right that we should bind to the instance name those class ?. I mean not binding to the name / definition of the class ?.

    Typo here. is it right that we should bind to the instance name those class ?

Sign In or Register to comment.