R
Roman Ivasyk
Guest
I have the following code C# code:
using System.ComponentModel;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _textPropertyToBind;
public string TextPropertyToBind
{
get { return _textPropertyToBind; }
set
{
_textPropertyToBind = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TextPropertyToBind)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
}
The view looks like this:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400">
<Grid>
<TextBox Text="{Binding Path=TextPropertyToBind, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Height="30" Width="120" FontSize="20"/>
</Grid>
</Window>
As you can see Text property of TextBox control bound to TextPropertyToBind and UpdateSourceTrigger is PropertyChanged what means TextPropertyToBind will be updated as soon as we make any change to the TextBox. For the English keyboard layout, it works fine - the setter of the TextPropertyToBind is called once per change, BUT if we choose Chinese(Simplified, China (Microsoft Pinyin)) the setter will be called twice per any single change except Backspace. Why is this happening? How to avoid this behavior?
Continue reading...
using System.ComponentModel;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _textPropertyToBind;
public string TextPropertyToBind
{
get { return _textPropertyToBind; }
set
{
_textPropertyToBind = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TextPropertyToBind)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
}
The view looks like this:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400">
<Grid>
<TextBox Text="{Binding Path=TextPropertyToBind, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Height="30" Width="120" FontSize="20"/>
</Grid>
</Window>
As you can see Text property of TextBox control bound to TextPropertyToBind and UpdateSourceTrigger is PropertyChanged what means TextPropertyToBind will be updated as soon as we make any change to the TextBox. For the English keyboard layout, it works fine - the setter of the TextPropertyToBind is called once per change, BUT if we choose Chinese(Simplified, China (Microsoft Pinyin)) the setter will be called twice per any single change except Backspace. Why is this happening? How to avoid this behavior?
Continue reading...