This is the code to reproduce this issue:
xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Text="{Binding Num, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox>
</Grid>
C#:
using System.ComponentModel;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new Entity();
}
}
public class Entity : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double num;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public double Num
{
get { return num; }
set
{
num = value;
if (value > 100)
{
num = 100;
}
OnPropertyChanged("Num");
}
}
}
}
Now, if I run it, input 1, it's ok. Then input another 1, which makes it 11, it's still ok. And then input another more 1, which makes 111, now the validation will work and change the value to 100, and the UI will show 100. But then if I input more numbers, the UI will not change, it'll be 1001.
I guess it has something to do with setting the property to the same value(100) for twice. But I have no idea how to fix it, by fix it, I mean to make the UI always follow the property value.
Thanks
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--The Too tip for the textbox-->
<Style x:Key="txterror" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"></Setter>
<Setter Property="Background" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox x:Name="txt" Text="{Binding Mode=TwoWay, IsAsync=True, Path=Num, UpdateSourceTrigger=PropertyChanged}" Margin="63,36,71,184" >
<TextBox.Effect>
<DropShadowEffect ShadowDepth="5"/>
</TextBox.Effect>
</TextBox>
<TextBox Margin="63,96,71,124" Text="{Binding ElementName=txt,Path=Text}">
<TextBox.Effect>
<DropShadowEffect ShadowDepth="5" />
</TextBox.Effect>
</TextBox>
</Grid>
</Window>
Use above code its working fine logicaly. means that when u set value of Num proeprty it setting proerty value to 100 but the textbox values of your widnow is not changed. it is because of the framework element it uses the Error validation so what so ever u want to put into textbox it will type that and present that value on the textbox. but the property values is always 100 when ever its goes higher then 100.
Your Solution is to use the IsAsync=True in you binding statement
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With