【发布时间】:2018-11-25 18:17:35
【问题描述】:
有没有办法在 XAML 创建的文本框中获取之前的文本值,然后再更改它?
我的目标是让用户在文本框中输入输入值,检查它是数字还是以“.”开头。如果不是,则将文本框值返回到之前的数字。
XAML 代码:
<Label x:Name="Label_F" Content="F" Margin="5" VerticalAlignment="Center" Width="20"/>
<TextBox x:Name="Box_F" Text="{Binding F, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IntegralCageCheck, Converter={StaticResource booleaninverter}}" Margin="5" Width="50" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
代码
public string F
{
get { return _F; }
set
{
_F = value;
double temp;
bool result = double.TryParse(value, out temp);
if (!char.IsDigit(Convert.ToChar(_F)) && _F != ".")
{
_F = _F.previousvalue; // Using as example
}
else { FrontPanelVariables.F = temp * 25.4; }
RaisePropertyChanged("F");
【问题讨论】:
标签: c# binding textbox textchanged