本文介绍了将3个文本框绑定在一起;相同的DateTime格式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我现在有三个文本框:<TextBox Name="SDate1" Text="{Binding Source={x:Static System:DateTime.Now}, Mode=OneWay, StringFormat='MM/dd/yyyy'}" /><TextBox Name="SDate2" /><TextBox Name="STime1" Text="{Binding Source={x:Static System:DateTime.Now}, Mode=OneWay, StringFormat='hh:mm:ss'}" />在代码背后:SDate2.Text = String.Format("{0},{1}/{2}", now.Year, now.DayOfYear.ToString("d3"), now.ToString("HHmmss"));这是什么样子:我想做的是,如果我编辑一个文本框的任何部分,它应该编辑其他的在适当的部分。所以如果我将第一个文本框中的 2016 部分更改为 2017 , SDate2 像这样:What I would like to do is, if I edit any part of one textbox, it should edit the others as well at the appropriate part. So If I changed the 2016 part in the first text box to 2017, the SDate2 like so:如果我更改小时/ STime 中的分钟/秒,这将更改 SDate2 的最后部分,反之亦然。This should also work if I change the hours/minutes/seconds in STime which will change the last part of SDate2 and vice versa.更改 SDate1 中的日期将更改中的 dayOfYear SDate2 ,反之亦然。Changing the day in SDate1 will change the dayOfYear in SDate2 and vice versa.什么是实现这样的最佳方式?What would be the best way to achieve something like this?编辑:我可以将文本框绑定在一起,但格式不保留,并且在所有文本框中都是相同的文本。 I can bind the textboxes together, but the format isn't kept, and it's the same text in all the textboxes.编辑2:这是我尝试的xaml.cs文件中的代码。Edit 2: Here's the code behind in the xaml.cs file I tried.public partial class TestDate : Window, INotifyPropertyChanged{ private DateTime _dateInViewModel; public TestDate() { InitializeComponent(); } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, e); } } public DateTime DateInViewModel { get { return _dateInViewModel; } set { _dateInViewModel = value; OnPropertyChanged(new PropertyChangedEventArgs("DateInViewModel")); } }}这是XAML的更新文本框:Here's the update XAML for the textboxes:<TextBox Name="SDate1" Text="{Binding DateInViewModel, StringFormat='MM/dd/yyyy'}" /><TextBox Name="SDate2" Text="{Binding DateInViewModel}" /><TextBox Name="STime1" Text="{Binding DateInViewModel, StringFormat='hh:mm:ss'}" />推荐答案这不行的原因是因为你有约束力分隔 DateTime 实例。所有三个 TextBox es应绑定到相同的 DateTime 实例。在一个合适的MVVM实现中,你应该有一个视图模型,它具有一个 DateTime 属性,可以调用它 DateInViewModel :The reason this is not working is because you are binding to separate DateTime instances. All three TextBoxes should bind to the same DateTime instance. In a proper MVVM implementation you should have a viewmodel with a DateTime property, lets call it DateInViewModel:private DateTime _dateInViewModel;public DateTime DateInViewModel{ get { return _dateInViewModel; } set { _dateInViewModel = value; NotifyPropertyChanged("DateInViewModel"); }} viewmodel应该实现 INotifyPropertyChanged 界面,所以当属性改变时通知绑定。The viewmodel should implement the INotifyPropertyChanged interface, so the bindings are notified when the property is changing.而不是设置 SDate2 的文本到 string.Format(...)你只需设置 DateInViewModel 在viewmodel中:Instead of setting SDate2's Text to that string.Format(...) you just set DateInViewModel in the viewmodel:DateInViewModel = DateTime.Now;最后你的XAML:<TextBox Name="SDate1" Text="{Binding DateInViewModel, StringFormat='MM/dd/yyyy'}" /><TextBox Name="SDate2" Text="{Binding DateInViewModel}" /><TextBox Name="STime1" Text="{Binding DateInViewModel, StringFormat='hh:mm:ss'}" />您可以将绑定的 UpdateSourceTrigger 更改为 PropertyChanged 可在您编辑时查看即时更新。 TextBox 的文本绑定的默认 UpdateSourceTrigger LostFocus 所以其他绑定只有当您点击 TextBox 之外时才会收到通知。You can change the bindings' UpdateSourceTrigger to PropertyChanged to see instant updates as you edit. The default UpdateSourceTrigger for the TextBox's Text binding is LostFocus so other bindings are only notified when you click outside the TextBox. 这篇关于将3个文本框绑定在一起;相同的DateTime格式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 05:20