在ObservableCollection

时间:2017-10-19 01:34:33

标签: c# wpf data-binding timer binding

我有一个ObservableCollection<Tuple<RandomClass,TimerPlus>>,其中TimerPlusSystem.Timers.Timer的扩展版本,并且有一个名为&#34; TimeLeft&#34;的DateTime属性,它返回计时器剩余的时间。

我有一个ItemsControl,它绑定到observable集合。我绑定了元组的第1项中的一些属性,我还想绑定到TimerPlus(Item2)中的DateTime TimeLeft。但是,绑定有效,不会使用新值进行更新。

在TimerPlus内部我实现了INotifyPropertyChanged,当在TimerPlus上调用Start()时,它每秒启动一个DispatcherTimer来引发OnPropertyChanged("TimeLeft"),但这不起作用

如果没有这样做,我如何让ItemsControl每隔一段时间拉出新的剩余时间?

XAML(对一些不重要的代码进行了编辑)

<ItemsControl VerticalContentAlignment="Stretch" ItemsSource="{Binding WaitingMarkets}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="#FF424242" BorderThickness="3" Margin="5">
                <StackPanel>
                    <TextBlock FontWeight="Bold" FontSize="18" Margin="5" HorizontalAlignment="Center">
                                    <Run Text="Market ID: "
                                         /><Run Text="{Binding Item1.MarketId}" />
                    </TextBlock>
                    <Separator Margin="5" />
                    <TextBlock Margin="5">
                        <Run Text="Time Remaining: "
                        /><Run Text="{Binding Item2.TimeLeft, StringFormat='HH:mm:ss'}" />
                    </TextBlock>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

MarketID从item1和item2正确绑定TimeLeft最初绑定正确,但不会更新。

我所拥有的第2项课程如下:

public class TimerPlus : System.Timers.Timer, INotifyPropertyChanged
{
    private DateTime m_dueTime;
    private DispatcherTimer ClockTimer;

    public TimerPlus() : base()
    {
        this.Elapsed += this.ElapsedAction;
    }

    protected new void Dispose()
    {
        this.Elapsed -= this.ElapsedAction;
        base.Dispose();
    }

    public DateTime TimeLeft
    {
        get
        {
            return new DateTime(2017,1,1) + (this.m_dueTime - DateTime.Now);
        }
        set { }
    }

    public new void Start()
    {
        this.m_dueTime = DateTime.Now.AddMilliseconds(this.Interval);
        ClockTimer = new DispatcherTimer(DispatcherPriority.Render);
        ClockTimer.Interval = TimeSpan.FromSeconds(1);
        ClockTimer.Tick += (sender, args) =>
        {
            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { OnPropertyChanged("TimeLeft"); }));
        };
        ClockTimer.Start();
        base.Start();
    }

    private void ElapsedAction(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (this.AutoReset)
        {
            this.m_dueTime = DateTime.Now.AddMilliseconds(this.Interval);
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            if (handler.Target is CollectionView)
            {
                ((CollectionView)handler.Target).Refresh();
            }
            else
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    #endregion
}

我在没有Dispatcher.Invoke的情况下尝试了这个,并尝试传递&#34; Item2.TimeLeft&#34;作为OnPropertyChanged的参数。

0 个答案:

没有答案