Reputation: 1163
I have a content view with several controls in it, which I want to bind to the properties of a property in the content view's code behind. The model is passed correctly to the content view, but the bindings are not updated.
The content view's XAML is:
<ContentView.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="clock" />
<Label Grid.Column="1" Text="{Binding Summary.MeetingsList}" />
<StackLayout Grid.Column="2">
<Image Source="meeting" />
<Label Text="{Binding Summary.TotalMettings}"/>
</StackLayout>
<StackLayout Grid.Column="3">
<Image Source="people"/>
<Label Text="{Binding Summary.People}" />
</StackLayout>
</Grid>
</ContentView.Content>
The content view's code behind:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SummaryControl: ContentView
{
public static readonly BindableProperty SummaryProperty = BindableProperty.Create(nameof(Summary), typeof(SummaryModel), typeof(SummaryModel), new SummaryModel(), BindingMode.OneWay,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
var view = bindableObject as SummaryControl:
view.Summary = (SummaryModel)newValue;
});
public SummaryModel Summary { get { return (SummaryModel)GetValue(SummaryProperty); } set { SetValue(SummaryProperty, value); } }
public SummaryControl: ()
{
InitializeComponent();
}
}
The SummaryModel class:
public class SummaryModel
{
public string TotalMettings { get; set; }
public string People { get; set; }
public string MeetingsList { get; set; }
}
Do I need another class as my viewmodel for the content view?
Upvotes: 1
Views: 1127
Reputation: 18861
It seems that you forget to set the binding path
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="CustomView" // set the name of view
x:Class="xxx">
<Label Text="{Binding Summary.MeetingsList,Source={x:Reference CustomView}}" />
Upvotes: 1