我有一个使用tabcontrol的wpf应用程序。结构如下所示:
/*MainView*/
<UserControl>
<UserControl.Resources>
<DataTemplate DataType="{x:Type tapViewModels:TabViewModel}">
<tabViews:TabView />
</DataTemplate>
</UserControl.Resources>
<ScrollViewer Name="MainScrollViewer" VerticalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
...
</Grid>
<Grid Grid.Row="1">
<dx:DXTabControl ItemsSource="{Binding TabItems}" SelectedIndex="0">
<dx:DXTabControl.ItemHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Header}" />
</DataTemplate>
</dx:DXTabControl.ItemHeaderTemplate>
<dx:DXTabControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Path=Content}" /> /*Content = new TabViewModel()*/
</DataTemplate>
</dx:DXTabControl.ItemTemplate>
</dx:DXTabControl>
</Grid>
</Grid>
</ScrollViewer>
</UserControl>
/*TabView*/
<UserControl>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
...
</Grid>
<Grid Grid.Row="1">
<dxg:GridControl>
<dxg:TableView>
...
</dxg:TableView>
</dxg:GridControl>
</Grid>
</Grid>
</UserControl>
当我在悬停GridControl的同时向上滚动时,我想要滚动事件到&#34; bubble&#34;如果GridControl已经全部滚动到顶部。当然,当在底部向下滚动时也一样。
我的问题是,GridControl和ScrollViewer存在于两个不同的视图中,并且具有两个不同的ViewModel。那么我怎样才能做到这一点呢?
我会先生。一个MVVM解决方案,但在这一点上,我可以做任何事情!
答案 0 :(得分:0)
您可以在较低控件上处理MouseWheel事件,并检查它是否滚动到顶部,然后引发上部控件的事件:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<wpfApplication3:ControlA x:Name="A" Grid.Row="0" />
<wpfApplication3:ControlB x:Name="B" Grid.Row="1" PreviewMouseWheel="HandleMouseWheel"/>
</Grid>
然后在HandleMouseWheel事件中:
private void HandleMouseWheel(object sender, MouseWheelEventArgs e)
{
var controlB = sender as ControlB;
if (controlB != null && e.Delta > 0 && controlB.Scroll.VerticalOffset == 0)
{
e.Handled = true;
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent,
Source = sender
};
A.Scroll.RaiseEvent(eventArg);
}
}
e.Delta检查您是否正在向上移动并且VerticalOffset检查确保控件已滚动到顶部。
这假设您的usercontrols中有一个公开可用的scrollviewer,例如:
<UserControl>
<ScrollViewer x:Name="Scroll">
<Grid Height="1000"></Grid>
</ScrollViewer>
</UserControl>