大佬教程收集整理的这篇文章主要介绍了Silverlight DatePicker绑定损坏 – 我该怎么做才能解决这个问题?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个List@L_616_4@绑定到具有datetiR_322_11845@e属性的对象列表.我有一个页面的编辑部分用于更改所选项目.这很好 – 当我更新DatePicker中的日期时,List@L_616_4@显示我更新的日期.
但是,当我然后选择另一个项目时,DatePicker控件也会错误地更新新项目上的日期.
这是我的代码:
C#:
using System; using System.Collections.ObjectModel; using System.ComponentModel; namespace BindingTest { public partial class MainPage { public MainPage() { InitializeComponent(); var vm = new viewmodel(); DataContext = vm; } } public class viewmodel : INotifyPropertyChanged { public viewmodel() { List = new ObservableCollection<Item>(); for (var n = 0; n < 10; n++) List.Add(new Item { Date = datetiR_322_11845@e.Now.AddDays(n) }); } public ObservableCollection<Item> List { get; set; } private Item _SELEctedItem; public Item SELEctedItem { get { return _SELEctedItem; } set { _SELEctedItem = value; OnPropertyChanged("SELEctedItem"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(String propName) { if (PropertyChanged != null) PropertyChanged(this,new PropertyChangedEventArgs(propName)); } } public class Item : INotifyPropertyChanged { private datetiR_322_11845@e _date; public datetiR_322_11845@e Date { get { return _date; } set { _date = value; OnPropertyChanged("Date"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(String propName) { if (PropertyChanged != null) PropertyChanged(this,new PropertyChangedEventArgs(propName)); } } }
XAML:
<UserControl xmlns:sdk="http://scheR_322_11845@as.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="BindingTest.MainPage" xmlns="http://scheR_322_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_322_11845@as.microsoft.com/winfx/2006/xaml"> <Grid x:Name="LayoutRoot" BACkground="White"> <Grid.columnDeFinitions> <columnDeFinition /> <columnDeFinition /> </Grid.columnDeFinitions> <List@L_616_4@ Itemssource="{Binding List}" DisplaymemberPath="Date" SELEctedItem="{Binding SELEctedItem,Mode=TwoWay}" /> <StackPanel Grid.column="1" DataContext="{Binding SELEctedItem}"> <TextBlock Text="Date:" /> <sdk:DatePicker SELEctedDate="{Binding Date,Mode=TwoWay}" /> </StackPanel> </Grid> </UserControl>
我怎样才能解决这个问题?
xaml中的DataBinding:
<local:EvdDatePicker SELEctedDateEx="{Binding viewmodelProperty,Mode=TwoWay}"/>
DatePicker扩展:
/// <sumMary> /// Databinding on DatePicker.SELEctedDate is serIoUsly messed up (maybe because of synchronization with /// Text property?). This class extends the DatePicker and provides another property (SELEctedDateEX) /// to bind to. This offers decoupling and a BACkup of the date value that can be reverted to. /// Additionally,SELEcted date (of any EvdDatePicker instancE) may only be changed at a defined interval. /// </sumMary> public class EvdDatePicker : DatePicker { // allow changes only every half second (adjust if necessary) private static TimeSpan _changeLock = TimeSpan.FromMilliseconds(500); // holds date of last user change private static datetiR_322_11845@e _lastChange; public EvdDatePicker() { this.SELEctedDateChanged += new EventHandler<SELEctionChangedEventArgs>(EvdDatePicker_SELEctedDateChanged); } /// <sumMary> /// Catch cases where SELEctedDate gets changed by mistake /// </sumMary> void EvdDatePicker_SELEctedDateChanged(object sender,SELEctionChangedEventArgs E) { // measures if the change is likely caused by unwanted chain reactions if (_lastChange < datetiR_322_11845@e.Now.Subtract(_changeLock)) { this.SELEctedDateEx = e.AddedItems.Count > 0 ? (datetiR_322_11845@e?)e.AddedItems[0] : null; _lastChange = datetiR_322_11845@e.Now; // store last change time } // reject change (revert to old value),if the values are not synchronized by Now if (this.SELEctedDate != this.SELEctedDateEX) this.SELEctedDate = this.SELEctedDateEx; } /// <sumMary> /// Bind to this property instead of SELEctedDate /// </sumMary> public datetiR_322_11845@e? SELEctedDateEx { get { return (datetiR_322_11845@e?)GetValue(SELEctedDateExProperty); } set { SETVALue(SELEctedDateExProperty,value); } } public static readonly DependencyProperty SELEctedDateExProperty = DependencyProperty.Register("SELEctedDateEx",typeof(datetiR_322_11845@e?),typeof(EvdDatePicker),new PropertyMetadata(null,new PropertyChangedCallBACk(OnSELEctedDateExChanged))); private static void OnSELEctedDateExChanged(DependencyObject d,DependencyPropertyChangedEventArgs E) { EvdDatePicker p = (EvdDatePicker)d; // initial binding,propagate to SELEctedDate property datetiR_322_11845@e? newValue = (datetiR_322_11845@e?)e.NewValue; if (p.SELEctedDate != newvalue) p.SELEctedDate = newValue; } }
以上是大佬教程为你收集整理的Silverlight DatePicker绑定损坏 – 我该怎么做才能解决这个问题?全部内容,希望文章能够帮你解决Silverlight DatePicker绑定损坏 – 我该怎么做才能解决这个问题?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。