Bug 49237 - Bindable two way property in contentview (user control) fails to find property in view model
Summary: Bindable two way property in contentview (user control) fails to find propert...
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Chris King
URL:
Depends on:
Blocks:
 
Reported: 2016-12-07 17:32 UTC by TimeSlice
Modified: 2017-06-19 22:51 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Contenpage, ContentView, Viewmodel files from VS2015 project (2.92 KB, application/x-zip-compressed)
2016-12-07 17:32 UTC, TimeSlice
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description TimeSlice 2016-12-07 17:32:02 UTC
Created attachment 18812 [details]
Contenpage, ContentView, Viewmodel files from VS2015 project

My objective is to create a user control (IE: View) containing a basic Entry control that will update a specified property in my view model.

The user control bindableproperty fails to find the bound property name in the view model.
My expectation is that I should be able to add one or more of my UserEditors to a page and bind it to various string properties in the view model, and the property the UserEditor gets/sets should be the property I specify in the {Binding } for the UserEditor.

I have attached a zip file containing the relevant user control (Contentview), Content page that consumes the user control, and the view model bound to the content page.

Note the specific error I see in the VS output window indicates the user control fails to find the property in the viewmodel with the name specified in <local:UserEditor x:Name="UserEditor" Text="{Binding TextPropertyInVm, Mode=TwoWay}"></local:UserEditor>

[0:] Binding: 'Text' property not found on 'UserControl.MainPageViewModel', target property: 'Xamarin.Forms.Entry.Text'

The viewmodel contains a property named TextPropertyInVm which is what is specified in the binding.  Internally within the user control the Entry is bound to a property named Text, but nonetheless, I expect it should be respecting the property name specified in Binding in the content page.
Comment 1 TimeSlice 2016-12-08 17:19:43 UTC
It seems that it is necessary to set the binding context of the Entry control in UserEditor.cs to the UserEditor.  This resolves the problem, however I'm not entirely sure this should be necessary.  Would appreciate feedback on this solution.

The revised constructor in UserEditor.cs becomes:

public UserEditor()
{
    InitializeComponent();
    TextEntry.BindingContext = this;  
}
Comment 2 Chris King 2017-02-16 19:54:20 UTC
TimeSlice, please direct "best practice" and "how to" questions to Stack Overflow or the forums. You can add a link back here so we can find your question.
Comment 3 Samantha Houts [MSFT] 2017-06-19 22:51:08 UTC
Closing per Comment 2. Thank you.