TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Patrick Rainer
NA
35
2.6k
Xamarin, Bindable Property
Oct 23 2020 6:07 AM
Dear all
It looks like I do misunderstand something in bindable properties. I would appreciate if somebody could explain me, how to do it correctly and why it works like that.
Why does the Binding on the "MeetingControl for "Meeting.Subject" not work?
GitHub Link: https://github.com/PatrickRainer/BindablePropertyExample
This is my Main Page:
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<ContentPage x:Name=
"MyPage"
xmlns=
"http://xamarin.com/schemas/2014/forms"
xmlns:x=
"http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local=
"clr-namespace:SimpleSfTimePicker"
x:Class=
"SimpleSfTimePicker.MainPage"
BindingContext=
"{x:Reference Name=MyPage}"
>
<StackLayout BackgroundColor=
"LightCyan"
>
<Entry Text=
"{Binding Meeting.Subject}"
/>
<local:MeetingControl x:Name=
"MeetingControl"
Subject=
"{Binding Meeting.Subject}"
/>
</StackLayout>
</ContentPage>
This is the Code Behind (For ease I used no Vm):
public
partial
class
MainPage {
Meeting _meeting;
public
MainPage() {
InitializeComponent();
Meeting =
new
Meeting {
Subject =
"A Meeting"
,
};
}
public
Meeting Meeting {
get
=>_meeting;
set
{
_meeting = value;
OnPropertyChanged();
}
}
}
The Meeting:
public
sealed
class
Meeting: INotifyPropertyChanged {
string
_subject;
public
string
Subject {
get
=>_subject;
set
{
if
(value == _subject)
return
;
_subject = value;
OnPropertyChanged();
}
}
public
event
PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
void
OnPropertyChanged([CallerMemberName]
string
propertyName =
null
) {
PropertyChanged ? .Invoke(
this
,
new
PropertyChangedEventArgs(propertyName));
}
}
The Meeting Control Xaml:
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<ContentView x:Name=
"MyContentView"
xmlns=
"http://xamarin.com/schemas/2014/forms"
xmlns:x=
"http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:til=
"clr-namespace:Syncfusion.XForms.TextInputLayout;assembly=Syncfusion.Core.XForms"
x:Class=
"SimpleSfTimePicker.MeetingControl"
BindingContext=
"{x:Reference MyContentView}"
>
<StackLayout>
<til:SfTextInputLayout
Hint=
"Subject"
>
<Entry x:Name=
"SubjectEntry"
/>
</til:SfTextInputLayout>
</StackLayout>
</ContentView>
The MeetingControl Code Behind:
[XamlCompilation(XamlCompilationOptions.Compile)]
public
partial
class
MeetingControl {
public
static
readonly
BindableProperty SubjectProperty = BindableProperty.Create(nameof(Subject),
typeof
(
string
),
typeof
(MeetingControl),
"default"
, BindingMode.TwoWay,
null
, PropertyChanged);
static
void
PropertyChanged(BindableObject bindable,
object
oldvalue,
object
newvalue) {
var str = (
string
) newvalue;
var control = (MeetingControl) bindable;
control.SubjectEntry.Text = str;
}
public
string
Subject {
get
{
return
(
string
) GetValue(SubjectProperty);
}
set
{
SetValue(SubjectProperty, value);
}
}
public
MeetingControl() {
InitializeComponent();
}
}
Reply
Answers (
2
)
Is it possible to create UI control other then main thread
How to save and clean a specific database if there are 2 or more data?