Binding to a UserControl's property which is bound to VM
Posted: 23 Jul 2021, 17:43
Hi folks, I'm investigating bindings mechanics rn and don't understand why it doesn't play nicely with UserControls. Here's my simplified code:
My View:
LabeledInputBox UserControl is just a TextBox with some properties and visual stuff attached:
TheVM ViewModel class doesn't have anything fancy as well:
If it's relevant, TheVM is stored in a parent VM which is inherited from NotifyPropertyChangedBase and declared like this:
Now, I'm continuously polling the Scale property value on TheVM object from code while typing into TextBox/d:LabeledInputBox. The property gets updated properly when TextBox is used, but not with d:LabeledInputBox, although it gets the default Scale value if I set one. What am I doing wrong?
My View:
Code: Select all
<Grid DataContext="{Binding TheVM}">
<!-- THIS WORKS -->
<!-- <TextBox Text="{Binding Scale}" /> -->
<!-- THIS DOESN"T WORK -->
<d:LabeledInputBox Text="{Binding Scale}" />
</Grid>
Code: Select all
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="clr-namespace:D"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="D.LabeledInputBox">
<Grid>
<!-- ...other stuff -->
<TextBox Text="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type d:LabeledInputBox}}}}" />
</Grid>
</UserControl>
Code: Select all
NS_IMPLEMENT_REFLECTION(LabeledInputBox, "D.LabeledInputBox")
{
Noesis::DependencyData* data = NsMeta<Noesis::DependencyData>(Noesis::TypeOf<SelfClass>());
data->RegisterProperty<Noesis::String>(TextProperty,
"Text",
Noesis::PropertyMetadata::Create(Noesis::String{}));
}
Code: Select all
NS_IMPLEMENT_REFLECTION(TheVM, "D.TheVM") // inherited from Noesis::DependencyObject
{
Noesis::DependencyData* data = NsMeta<Noesis::DependencyData>(Noesis::TypeOf<SelfClass>());
data->RegisterProperty<Noesis::String>(
ScaleProperty, "Scale", Noesis::PropertyMetadata::Create(Noesis::String{}));
}
Code: Select all
NsProp("TheVM", &ParentVM::GetTheVM);