question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bindings to Text property in TextBox doesnt work

See original GitHub issue

I 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>

bandicam-2020-02-22-12-52-12-586

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
grokyscommented, Feb 23, 2020

Hmm, that line was added by https://github.com/AvaloniaUI/Avalonia/commit/a222fa9b0c2c14380db40d98dd553f2d744517f2:

Don’t modify text due to binding when typing.

If a TextBox is e.g. bound to an int and the user types “02” then the TextBox text should be “02” - not the value that comes back from the binding which will be “2”.

This was added in #691 back in 2016. However cross-checking with WPF, WPF doesn’t seem to have this behavior. If a TextBox is 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.

0reactions
grokyscommented, Feb 23, 2020

Repoening because this needs investigation.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found