WPF——如何将事件从集合类冒泡到主窗口
我正在使用ObjectDataProvider调用具有IObservableCollection的类:WPF——如何将事件从集合类冒泡到主窗口,wpf,polling,objectdataprovider,Wpf,Polling,Objectdataprovider,我正在使用ObjectDataProvider调用具有IObservableCollection的类: <ObjectDataProvider x:Key="WaitingPatientDS" ObjectType="{x:Type local:clsPatients}"> <ObjectDataProvider.ConstructorParameters> <sys:Boolean>True</sys:Bo
<ObjectDataProvider x:Key="WaitingPatientDS"
ObjectType="{x:Type local:clsPatients}">
<ObjectDataProvider.ConstructorParameters>
<sys:Boolean>True</sys:Boolean>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
符合事实的
clsPatient获取数据并填充集合。它还使用计时器按时间间隔轮询重新查询数据库
问题:如何在clsPatient中为StartPoll和EndPoll创建事件,更重要的是,如何将这些事件冒泡到我的WPF窗口的codebehind中?我不清楚是什么连接到了谁以及如何连接,所以让我告诉你我将如何做 ODP构造了一个clsPatients实例,其中包含一个充满“数据”的“集合” clsPatients还有一个Dispatchermer,它定期更新数据属性并激发PropertyChanged
public void DispatcherTimerTick(object sender, EventArgs e)
{
Data = new BindingList(Repository.GetMyDataLol());
// standard event firing method here, move along:
OnPropertyChanged("Data");
}
在UI中,我将绑定此集合(这可能是无bug的,也可能不是):
并更新计时器滴答声事件:
public void DispatcherTimerTick(object sender, EventArgs e)
{
Loading = Visibility.Visible;
OnPropertyChanged("Loading");
Data = new BindingList(Repository.GetMyDataLol());
OnPropertyChanged("Data");
Loading = Visibility.Hidden;
OnPropertyChanged("Loading");
}
然后,在ui中,将指示器的可见性属性绑定到加载:
<Grid DataContext="{Binding Data Source={StaticResource WaitingPatientDS}}">
<ItemsControl
ItemsSource="{Binding Data}">
<ItemsControl.Resources>
<DataTemplate>
<!-- yadda -->
</ItemsControl>
<Image Source="hurf.jpg" Visibility="{Binding Loading}"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
谢谢,但让我详细说明一下:我不在乎财产的变化。我想在计时器滴答声中触发两个事件。一个在数据拉取之前,一个在数据拉取之后——让我们称之为“BeforePoll”和“AfterPoll”。然后在GUI上,我想捕获这些事件或以某种方式绑定到它们,这样我就可以打开和关闭动画循环gif(ala,AJAX外观),这样用户就知道幕后发生了什么事情。。。再次感谢您的帮助…我明白了要点,但只需要更多具体的例子…好的…非常接近…您帮了我很大的忙,但看起来我从未收到第一个可见性为“可见”的OnPropertyChanged通知。我甚至在数据加载后进行了1秒钟的睡眠,以使动画保持更长时间…你能想到我做错了什么吗?嗯。。。。这是在UI线程中发生的,因此可能会阻止UI更新。。。
public Visibility Loading{get;private set}
public void DispatcherTimerTick(object sender, EventArgs e)
{
Loading = Visibility.Visible;
OnPropertyChanged("Loading");
Data = new BindingList(Repository.GetMyDataLol());
OnPropertyChanged("Data");
Loading = Visibility.Hidden;
OnPropertyChanged("Loading");
}
<Grid DataContext="{Binding Data Source={StaticResource WaitingPatientDS}}">
<ItemsControl
ItemsSource="{Binding Data}">
<ItemsControl.Resources>
<DataTemplate>
<!-- yadda -->
</ItemsControl>
<Image Source="hurf.jpg" Visibility="{Binding Loading}"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
public void UpdateData(Object stateInfo)
{
var disp = Dispatcher.CurrentDispatcher();
Loading = Visibility.Visible;
disp.Invoke(() => { OnPropertyChanged("Loading");});
// optional sleep here
Data = new BindingList(Repository.GetMyDataLol());
disp.Invoke(() => { OnPropertyChanged("Data");});
Loading = Visibility.Hidden;
disp.Invoke(() => { OnPropertyChanged("Loading");});
}