3

我正在尝试从附加属性更改 ListViewItem 的背景。以下代码一直有效,直到 ListViewItem 滚动出显示的 ListView。错误是计时器在已断开连接的 ListViewItem 上触发。

{System.Windows.Controls.ListViewItem: {DisconnectedItem}} System.Windows.Controls.ListViewItem

如何在 Visual Studio 2010 中测试断开连接的项目?

TIA

namespace Stargate_V
{
    // Using singleton pattern to create one time to be shared among all ListViewItem instances.
    public static class ListTimer
    {
        // Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the 
        // Dispatcher and a DispatcherPriority can be set on the DispatcherTimer. Timer runs in its own thread.
        private static readonly Timer listTimer;

        static ListTimer()
        {
            listTimer = new Timer { AutoReset = true, Enabled = true, Interval = 10 * 1000 };  // Interval in milliseconds
            listTimer.Elapsed += listTimer_Elapsed;
        }

        static void listTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (ListTimerEvent != null)
            {
                ListTimerEvent(sender, e);       
            }  
        }

        public static event EventHandler ListTimerEvent;
    }

    // Static classes can not implement an interface. (Hence : INotifyPropertyChanged can not be used).
    public static class ListViewItemBehavior 
    {

        // Hint: create this with "propa" then tab

        public static string GetMyValue(DependencyObject obj)
        {
            return (string)obj.GetValue(MyValueProperty);
        }

        public static void SetMyValue(DependencyObject obj, string value)
        {
            obj.SetValue(MyValueProperty, value);
        }

        // Using a DependencyProperty as the backing store for MyValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyValueProperty =
            DependencyProperty.RegisterAttached("MyValue", typeof(Object), typeof(ListViewItemBehavior), new UIPropertyMetadata(null, OnMyValueChanged));

        static void OnMyValueChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
        {
            var item = depObj as ListViewItem;
            if (item == null) return;

           // This is unable to pass the instance data:  ListTimer.ListTimerEvent +=new EventHandler(ListTimer_ListTimerEvent);
           // Use an anonymous method in the OnMyValueChanged to specify the timer event handler. This will give access to the specific 
           // ListViewItem.
            ListTimer.ListTimerEvent += (sender, e) =>
            {
                Timer timer = sender as Timer;

                // The Timer is running on a different thread then the ListViewItem item
                item.Dispatcher.Invoke((Action)(() =>
                {
--THIS FAILS WHEN ITEM HAS BEEN SCROLLED OUT OF VIEW AND DISCONNECTED ---
                    View_encountertime vt = item.DataContext as   View_encountertime;
                    item.Background = Brushes.Azure;

                    // convert checkin time to a  datetime.
                    var b = vt.Checkin;
                    DateTime z = (DateTime)vt.Tencounter;

                   // string sdt = DateTime.Now.ToShortDateString() + " " + values[0].ToString();
                   // DateTime checkin = DateTime.Parse(sdt);

                    // get current time.
                    // DateTime now = DateTime.Now;
                    DateTime now = new DateTime(2015, 2, 13, 11, 30, 5);
                    TimeSpan ts = now.Subtract(z);

                    if (ts.CompareTo(WaitingTime.ninetymin) < 1) item.Background = Brushes.Orange;          // orange
                    if (ts.CompareTo(WaitingTime.seventyfivemin) < 1) item.Background = Brushes.Yellow;     // yellow 
                    if (ts.CompareTo(WaitingTime.sixtymin) < 1) item.Background = Brushes.Green;            // green
                    if (ts.CompareTo(WaitingTime.fortyfivemin) < 1) item.Background = Brushes.Turquoise;    // turquose
                    if (ts.CompareTo(WaitingTime.thirtymin) < 1) item.Background = Brushes.Blue;            // blue
                    if (ts.CompareTo(WaitingTime.fifteenmin) < 1) item.Background = Brushes.Violet;         // violet


                }));


            };


        }
    }

    // Waiting times
    public static class WaitingTime
    {
        public static TimeSpan fifteenmin;
        public static TimeSpan thirtymin;
        public static TimeSpan fortyfivemin;
        public static TimeSpan sixtymin;
        public static TimeSpan seventyfivemin;
        public static TimeSpan ninetymin;

        static WaitingTime()
        {
            fifteenmin = new TimeSpan(0, 15, 0);
            thirtymin = new TimeSpan(0, 30, 0);
            fortyfivemin = new TimeSpan(0, 45, 0);
            sixtymin = new TimeSpan(0, 60, 0);
            seventyfivemin = new TimeSpan(0, 75, 0);
            ninetymin = new TimeSpan(0, 90, 0);
        }

    } // end class WaitingTime


}
4

1 回答 1

8

与 Visual Studio 的版本无关。与.NET 的版本有关。在 4.5 及更高版本中,您可以使用BindingOperations.DisconnectedSource. 在 4.5 之前,.NET 没有公开这个成员,所以你必须做一个字符串比较。

于 2015-08-02T02:24:04.773 回答