2
votes

i made a custom view which displays a text.

in TestView.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView 
    xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EodiRoad.TestView">
    <StackLayout>

        <Label Text="{Binding Test}"/>
        <Label Text="this is test view"/>

    </StackLayout>
</ContentView>

and the codebehind

in TestView.xaml.cs

    public partial class TestView : ContentView
    {


        public static BindableProperty TestProperty =
            BindableProperty.Create(
                propertyName: "Test",
                returnType: typeof(string),
                declaringType: typeof(TestView),
                defaultValue:"???"
            );

        public string Test
        {
            get
            { return (string)GetValue(TestProperty); }
            set
            {
                Debug.WriteLine("test setted value = " + (string)value);
                SetValue(TestProperty, value); 
            }
        }



        public TestView()
        {
            InitializeComponent();
            BindingContext = this;
        }
    }

and when i use this is some other page like this

<local:TestView Test="hjhkhjk"/>

it would work just fine. But when i bind a data to it

<local:TestView Test="{Binding post.uploader.username}"/>

then it wont desplay anything...

its not a problem that post.uploader.username value is wrong or some that kind of thing. because

<Label Text="{Binding post.uploader.username}"/>

i have this code right under that malfunctioning line and it just works fine too..

what am i doing wrong here? how to fix it ..?

1
You probably need to implement PropertyChanged functionality to let the UI know your value is updatedGerald Versluis
alright i'll try. but would that explain the reason why /// <local:TestView Test="hjhkhjk"/> /// this works and ///<local:TestView Test="{Binding post.uploader.username}"/> /// this doesnt??softmarshmallow
Because setting it to a static value just sets it once and that is it. While when using a binding the value gets the default value first (empty) and when the binding happens the value is updated. Because you're missing the property changed logic the value is never updated in the UIGerald Versluis
what if multiple ui element is using one property some like this in TestView.xaml /// <Label Text="{Binding Test.a}"/> <Label Text="{Binding Test.b}"/> <Label Text="{Binding Test.c}"/>/// how should i implement onpropertychanged like?softmarshmallow

1 Answers

0
votes

You can make binding only from BindableObject property object in your TestView class. For example, if you will write next code:

<local:TestView Test="{Binding post.uploader.username}" />

Your BindableObject object must have post property object, which has uploader property object, which has username property string