-
Hello, <!-- MyControl.axml -->
<ControlTemplate>
<TextBox HorizontalAlignment="Center" Text="{TemplateBinding Text, Mode=TwoWay}" />
</ControlTemplate> // MyControl.axml.cs
public string Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value); // won't be called
}
public static readonly StyledProperty<string> TextProperty =
AvaloniaProperty.Register<SizeSetter, string>(nameof(Text)); <!-- MyControlParent.axml -->
<views:MyControl
Text="{Binding Text}"
</ControlTemplate> // MyControlParentViewModel.cs
public string Text
{
get => _Text;
set
{
// won't be called
SetProperty(ref _Text, value);
// some methods
}
}
string _Text = "Test"; In such case, when text of TextBox in MyControl is changed, setter of Text in MyControlViewModel won't be called, however text of TextBox does set to "Test" initially. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I find my solution: you should add <!-- MyControl.axml -->
<ControlTemplate>
<TextBox HorizontalAlignment="Center" Text="{TemplateBinding Text, Mode=TwoWay}" />
</ControlTemplate> <!-- MyControlParent.axml -->
<views:MyControl
Text="{Binding Text, Mode=TwoWay}" // here must add mode value explicitly
</ControlTemplate> In such case property in view model will change as TemplatedControl's does. How strange. |
Beta Was this translation helpful? Give feedback.
-
you can set default binding mode in property declaration. |
Beta Was this translation helpful? Give feedback.
you can set default binding mode in property declaration.