Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ContentView with a Bindable property. This property is set from the main page and it is setting correctly as I am seeing it in PropertyChanged function. Now I want to bind this value to the property of ViewModel of ContentView.

I can see debug point coming on OnChange in View's DP when set from MainPage, but it is not setting ViewModel property.

Appreciate your help!

What I have tried:

MainView.xaml.cs

public static readonly BindableProperty ContentViewDependencyProperty = BindableProperty.Create(
			"ContentViewDp",
			typeof(string),
			typeof(MainView),
			null, propertyChanged:OnChange);

		public string ContentViewDp
		{
			get { return (string)GetValue(ContentViewDependencyProperty ); }
			set { SetValue(ContentViewDependencyProperty , value); }
		}
private static void OnChange(BindableObject bindable, object oldValue, object newValue)
		{

		}


MainView.xaml

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
			 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
			 x:Class="MyProject.MainView"
			 BackgroundColor="White"
			 xmlns:local="clr-namespace:MyProject.Views;assembly=MyProject.Mobile"
			 xmlns:vm="clr-namespace:MyProject.ViewModels;assembly=MyProject.Mobile"
			 NavigationPage.HasNavigationBar="False">
    <ContentView.BindingContext>
		<vm:MainViewModel/>
	</ContentView.BindingContext>
	<ContentView.Resources>
		<Style TargetType="local:MainView">
			<Setter Property="ContentViewDp" Value="{Binding ViewModelProperty, Mode=TwoWay}"/>
		</Style>
	</ContentView.Resources>
</ContentView>


ViewModel.cs:

private string _viewModelProperty;
		public string ViewModelProperty
		{
			get { return _viewModelProperty; }
			set { SetProperty(ref _viewModelProperty, value, nameof(ViewModelProperty)); }
		}
Posted
Updated 21-Sep-22 22:50pm

1 solution

you set the binding to:
XML
<vm:MainViewModel/>

But above you state the class/file name is ViewModel.cs
In the ContentView.xaml, use:
XML
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
			 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
			 x:Class="MyProject.MainView"
			 BackgroundColor="White"
			 xmlns:local="clr-namespace:MyProject.Views;assembly=MyProject.Mobile"
			 xmlns:vm="clr-namespace:MyProject.ViewModels;assembly=MyProject.Mobile"
			 NavigationPage.HasNavigationBar="False"

             x:DataType="vm:MainViewModel">

Also set XmlCompilationOptions.Compile in Assembly.cs.

Watch this video for more information on bindings & MVVM: Faster & Safer XAML with Compiled Bindings in Xamarin.Forms & .NET MAUI[^]

Also, I highly recommend using ... Watch this video as to why: Benefits of .NET MAUI over Xamarin/Xamarin.Forms - Live Stream Highlights[^]

James wotks for Microsoft, and Xamarin before that. He has other videos that will benefit you greatly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900