gpt4 book ai didi

wpf - 通过验证绑定(bind)到双字段

转载 作者:行者123 更新时间:2023-12-02 07:49:58 28 4
gpt4 key购买 nike

我正在尝试使用 UpdateSourceTrigger=PropertyChangedTextBox 绑定(bind)到某个对象的 double 属性。目标是在编辑期间立即验证输入的值是否在允许的范围内(如果不在则显示错误)。我想在模型级别实现验证,即通过 IDataErrorInfo

当我绑定(bind)到 int 属性时,一切都很好,但如果属性是 double ,则会出现令人沮丧的编辑行为:删除数字小数部分的最后一个有效数字后 - 小数分隔符会自动删除(带有所有可能的小数零)。例如,从数字“12.03”中删除数字“3”后,文本将更改为“12”而不是“12.0”。

请帮忙。

这里是示例代码:

MainWindow.xaml:

<Window x:Class="BindWithValidation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="80" Width="200" WindowStartupLocation="CenterOwner">

<StackPanel>
<TextBox Width="100" Margin="10" Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
</Window>

MainWindow.xaml.cs:

namespace BindWithValidation
{
public partial class MainWindow : Window
{
private UISimpleData _uiData = new UISimpleData();

public MainWindow()
{
InitializeComponent();
DataContext = _uiData;
}
}
}

UISimpleData.cs:

namespace BindWithValidation
{
public class UISimpleData : INotifyPropertyChanged, IDataErrorInfo
{
private double _doubleField = 12.03;

public double DoubleField
{
get
{
return _doubleField;
}
set
{
if (_doubleField == value)
return;

_doubleField = value;
RaisePropertyChanged("DoubleField");
}
}

public string this[string propertyName]
{
get
{
string validationResult = null;
switch (propertyName)
{
case "DoubleField":
{
if (DoubleField < 2 || DoubleField > 5)
validationResult = "DoubleField is out of range";
break;
}

default:
throw new ApplicationException("Unknown Property being validated on UIData");
}

return validationResult;
}
}

public string Error { get { return "not implemented"; } }

public event PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged(string property)
{
if ( PropertyChanged != null )
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}

最佳答案

The behavior of binding float values to a textbox has been changed from .NET 4 to 4.5. With .NET 4.5 it is no longer possible to enter a separator character (comma or dot) with ‘UpdateSourceTrigger = PropertyChanged’ by default.

Microsoft says, this (is) intended

If you still want to use ‘UpdateSourceTrigger = PropertyChanged’, you can force the .NET 4 behavior in your .NET 4.5 application by adding the following line of code to the constructor of your App.xaml.cs:

public App()  
{
System.Windows.FrameworkCompatibilityPreferences
.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
}

(Sebastian Lux - 从 here 逐字复制)

关于wpf - 通过验证绑定(bind)到双字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11223236/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com