Bindings to Text property in TextBox doesnt work
See original GitHub issueI don’t know why binding to Text doesnt work in TextBox https://github.com/pelmenstar1/test-projects/blob/master/AvaloniaBindingTest/ViewModels/MainWindowViewModel.cs
public class MainWindowViewModel : ViewModelBase
{
private string _str = "#0000";
public string String
{
get => _str;
set
{
if (value!= null && value.Length > 0 && value[0] == '#' && value.Length <= 5)
{
_str = value;
}
this.RaisePropertyChanged(nameof(String));
}
}
}
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:AvaloniaBindingTest.ViewModels;assembly=AvaloniaBindingTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaBindingTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="AvaloniaBindingTest">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<TextBox Text="{Binding String, Mode=TwoWay}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Window>

Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (9 by maintainers)
Top Results From Across the Web
Setting Binding in code to Text property on TextBox WPF ...
1 Answer 1 ... If TxtKey is the name of a TextBox inside your UserControl , then you are having problems because you...
Read more >TextBox Binding doesn't work while focused (when using ...
TextBox Binding doesn't work while focused (when using uwp datagrid) The problem is when the user clicks save (a button on the command...
Read more >unable to get a TextBlock Binding to connect to a backing ...
Text field after the values are set in the CS file, it works correctly, and the GETTER is called as expected. It is...
Read more >My Class Property is not updated from TextBox Text Binding in ...
Simply set the Binding property UpdateSourceTrigger to PropertyChanged, That will make sure whenever your TextBox Text changes the source will ...
Read more >Data Binding and it's Modes in WPF
In WPF different controls has different default data-binding modes. For example, TextBlock's Text property has one-way as default binding mode ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hmm, that line was added by https://github.com/AvaloniaUI/Avalonia/commit/a222fa9b0c2c14380db40d98dd553f2d744517f2:
This was added in #691 back in 2016. However cross-checking with WPF, WPF doesn’t seem to have this behavior. If a
TextBoxis bound to an int and the user types “02” then the textbox will display simply “2”.I think we need to double-check our binding behavior in such cases where the setter doesn’t preserve the value written to it.
Repoening because this needs investigation.