gpt4 book ai didi

c# - 如何在更正绑定(bind)属性后立即更新 TextBox?

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:56 24 4
gpt4 key购买 nike

我已经完成了从 TextBox 到 Property 的绑定(bind)。如果用户将日期值写入 TextBox,它应该会自动更正。 f.e.: 20.01.10 -> 20.01.2010

更正在属性设置 block 中完成:

public String DateOfBirth
{
get
{
if (patient.DateOfBirth != DateTime.MinValue)
return patient.DateOfBirth.ToString("dd.MM.yyyy");
else
return patient.BirthdayString;
}
set
{
string dateParsed = ValidateDatePart(value, false, true);
DateTime date = new DateTime();
DateTime.TryParse(dateParsed, out date);

patient.DateOfBirth = date;
patient.BirthdayString = dateParsed;

base.OnPropertyChanged("DateOfBirth");
}
}

patient.DateOfBirthpatient.BirthdayString 是我应用程序数据模型中的值。需要将生日作为字符串返回,因为用户应该能够输入生日的一部分,例如如果生日不完全为人所知,例如“12.1967”。我的问题是,如果用户输入类似“20.01.10”的日期,该日期会在属性中得到更正,但更正后的日期 (“20.01.2010”) 未设置到 TextBox。 TextBox 仍包含“20.01”。有人知道如何更新 TextBox 吗?绑定(bind)模式默认为TwoWay。

这是文本框的 XAML:

<TextBox Validation.ErrorTemplate="{StaticResource errorTemplate}" Style="{StaticResource NotEmptyTextBox}">
<Binding Path="OrderVM.Patient.DateOfBirth" ValidatesOnDataErrors="True" ValidatesOnExceptions="True" />
</TextBox>

最佳答案

试试看

Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"

关于c# - 如何在更正绑定(bind)属性后立即更新 TextBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3456641/

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