Silverlight DatePicker绑定损坏 – 我该怎么做才能解决这个问题?

Silverlight DatePicker绑定损坏 – 我该怎么做才能解决这个问题?,第1张

概述我有一个非常简单的绑定问题,DatePicker正在逃避我. 我有一个ListBox绑定到具有DateTime属性的对象列表.我有一个页面的编辑部分用于更改所选项目.这很好 – 当我更新DatePicker中的日期时,ListBox显示我更新的日期. 但是,当我然后选择另一个项目时,DatePicker控件也会错误地更新新项目上的日期. 这是我的代码: C#: using System;usin 我有一个非常简单的绑定问题,DatePicker正在逃避我.

我有一个ListBox绑定到具有DateTime属性的对象列表.我有一个页面的编辑部分用于更改所选项目.这很好 – 当我更新DatePicker中的日期时,ListBox显示我更新的日期.

但是,当我然后选择另一个项目时,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 = DateTime.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 DateTime _date;        public DateTime 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://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="BindingTest.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    <GrID x:name="LayoutRoot" Background="White">        <GrID.ColumnDeFinitions>            <ColumnDeFinition />            <ColumnDeFinition />        </GrID.ColumnDeFinitions>        <ListBox 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>

我怎样才能解决这个问题?

解决方法@H_404_31@@H_301_32@ 我在Silverlight 3项目中遇到了一个非常类似的错误(请参阅我在Craig的回答中的评论).经过大量的试验和错误,创建一个扩展的DatePicker解决了我的问题.没有赢得漂亮奖,但我想一个混乱的子类可以预期是一团糟.

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 DateTime _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 < DateTime.Now.Subtract(_changeLock))        {            this.SelectedDateEx = e.AddedItems.Count > 0 ? (DateTime?)e.AddedItems[0] : null;            _lastChange = DateTime.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 DateTime? SelectedDateEx    {        get { return (DateTime?)GetValue(SelectedDateExProperty); }        set { SetValue(SelectedDateExProperty,value); }    }    public static Readonly DependencyProperty SelectedDateExProperty =        DependencyProperty.Register("SelectedDateEx",typeof(DateTime?),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        DateTime? newValue = (DateTime?)e.NewValue;        if (p.SelectedDate != newValue)            p.SelectedDate = newValue;    }}
总结

以上是内存溢出为你收集整理的Silverlight DatePicker绑定损坏我该怎么做才能解决这个问题?全部内容,希望文章能够帮你解决Silverlight DatePicker绑定损坏 – 我该怎么做才能解决这个问题?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1007423.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存