I'm having trouble binding a value to a custom ContentView I made.
My MainPage uses CustomFrameView which is a ContentView.
My CustomFrameView has a string BindableProperty. This works fine when I manually enter a string, but fails when I try to use Binding. And I'm sure the binding should work because it works when used on a Label.
Anyone has an idea on why it does not work?
MainPage.xaml
<StackLayout>
<!-- DOES work -->
<Label Text="{Binding Name}"/>
<!-- DOES work-->
<controls:CustomFrameView TestName="Test456"/>
<!-- DOES NOT work-->
<controls:CustomFrameView TestName="{Binding Name}"/>
</StackLayout>
MainPage.xaml.cs
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
MainPageViewModel.cs
public partial class MainPageViewModel : ObservableObject
{
[ObservableProperty]
private string name;
public MainPageViewModel ()
{
Name = "Test123";
}
}
CustomFrameView.xaml.cs
public partial class CustomFrameView : ContentView
{
public string TestName
{
get => (string)GetValue(TestNameProperty);
set => SetValue(TestNameProperty, value);
}
public static readonly BindableProperty TestNameProperty =
BindableProperty.Create(
nameof(TestName),
typeof(string),
typeof(CustomFrameView),
"",
propertyChanged: SetTestNameProperty);
private static void SetTestNameProperty(BindableObject bindable, object oldValue, object newValue)
{
var vm = bindable.BindingContext as CustomFrameViewModel;
vm.TestName = (string) newValue;
}
public CustomFrameView()
{
InitializeComponent();
this.BindingContext = new CustomFrameViewModel();
}
}
CustomFrameViewModel.cs
public partial class CustomFrameViewModel : ObservableObject
{
[ObservableProperty]
private string testName;
}
