2011-05-31 64 views
2

我有多个DataGrids放置在ScrollViewer上。 这些DataGrid具有“height:auto”属性,这样我就可以隐藏滚动条并查看所有内容。 唯一的问题是DataGrid需要重点,所以我不能滚动ScrollViewer。 是一个属性,以保持焦点的ScrollViewer,但也保持了DataGrids的行为(所以我可以选择元素)?ScrollViewer的DataGrids阻​​止它滚动

谢谢!

回答

1

我跑过这个完全相同的问题,除了我的情况稍微复杂一点。

ProductDataGrid.xaml:

<UserControl x:Class="My.Control.ProductDataGrid" ...> 
    <Grid> 
     <Grid.RowDefinitions>...</Grid.RowDefinitions> 

     <TextBlock x:Name="Header" Grid.Row="0" ... /> 

     <DataGrid x:Name="ProductData" Grid.Row="1" ... /> 
    </Grid> 
</UserControl> 

ProductPortfolioListView.xaml:

<Page ... 
     xmlns:my="clr-namespace:My.Control" 
     ....> 
    <Grid> 
     <Grid.RowDefinitions>...</Grid.RowDefinitions> 

     <ScrollViewer x:Name="ProductScrollViewer"> 
      <StackPanel> 
       <my:ProductDataGrid ... /> 

       <my:ProductDataGrid ... /> 

       <my:ProductDataGrid ... /> 
      </StackPanel> 
     </ScrollViewer> 
而是在ScrollViewer中有数据网格的,我在ScrollViewer中有一堆用户控件(称为ProductDataGrid和定义见下文)的

由Livsi提供的解决方案是正确的,但我的UserControl无法访问我的ScrollViewer,所以这里是我的解决方案:

ProductPortfolioListView.xaml:

<Page ... 
     xmlns:my="clr-namespace:My.Control" 
     ....> 
    <Grid> 
     <Grid.RowDefinitions>...</Grid.RowDefinitions> 

     <ScrollViewer x:Name="ProductScrollViewer"> 
      <StackPanel> 
       <my:ProductDataGrid ... 
         PreviewMouseWheel="ProductDataGrid_PreviewMouseWheel" /> 

       <my:ProductDataGrid ... 
         PreviewMouseWheel="ProductDataGrid_PreviewMouseWheel" /> 

       <my:ProductDataGrid ... 
         PreviewMouseWheel="ProductDataGrid_PreviewMouseWheel" /> 
      </StackPanel> 
     </ScrollViewer> 

ProductPortfolioListView.xaml.cs:

void ProductDataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs args) 
{ 
    ProductScrollViewer.ScrollToVerticalOffset(ProductScrollViewer.ContentVerticalOffset - args.Delta; 
    args.Handled = true; 
} 

注意这个解决方案的美在于事实,我可以在我的DataGrid从页面将保存它们分开,因此我实现了代码隔离以及更少的重复代码。甚至更好的是,我绝对利用了RoutedEvents继续从Source到它的所有父母的事件,直到有人处理它(在我的情况下是我的ProductScrollViewer)。

0

尝试在DataGrid中的CanContentScroll设置为False这样的:

<DataGrid ScrollViewer.CanContentScroll="False" ... /> 
+1

我已经尝试过,但这并没有帮助...无论如何,谢谢你! – 2011-05-31 10:58:33

5

它的后期,但我决定用这种方式这个问题: 我创建了PreviewMouseWheel事件的DataGrid 和手动滚动包装ScrollViewer

private void dgInvoicesItems_PreviewMouseWheel(object sender, MouseWheelEventArgs e) 
{ 
this.scrInvoice.ScrollToVerticalOffset(this.scrInvoice.ContentVerticalOffset - e.Delta); 
} 
0

TopMouseScrollPriorityBehavior。TopMouseScrollPriority

您可以在以下附加属性简单地设置您的ScrollViewer

public class TopMouseScrollPriorityBehavior 
{ 
    public static bool GetTopMouseScrollPriority(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(TopMouseScrollPriorityProperty); 
    } 

    public static void SetTopMouseScrollPriority(DependencyObject obj, bool value) 
    { 
     obj.SetValue(TopMouseScrollPriorityProperty, value); 
    } 

    public static readonly DependencyProperty TopMouseScrollPriorityProperty = 
     DependencyProperty.RegisterAttached("TopMouseScrollPriority", typeof(bool), typeof(TopMouseScrollPriorityBehavior), new PropertyMetadata(false, OnPropertyChanged)); 

    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var scrollViewer = d as ScrollViewer; 
     if (scrollViewer == null) 
      throw new InvalidOperationException($"{nameof(TopMouseScrollPriorityBehavior)}.{nameof(TopMouseScrollPriorityProperty)} can only be applied to controls of type {nameof(ScrollViewer)}"); 
     if (e.NewValue == e.OldValue) 
      return; 
     if ((bool)e.NewValue) 
      scrollViewer.PreviewMouseWheel += ScrollViewer_PreviewMouseWheel; 
     else 
      scrollViewer.PreviewMouseWheel -= ScrollViewer_PreviewMouseWheel; 
    } 

    private static void ScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) 
    { 
     var scrollViewer = (ScrollViewer)sender; 
     scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta); 
     e.Handled = true; 
    } 
} 

使用

<ScrollViewer b:TopMouseScrollPriorityBehavior.TopMouseScrollPriority="True" VerticalScrollBarVisibility="Auto" Margin="5" PanningMode="VerticalFirst"> 
    <DataGrid ScrollViewer.PanningMode="None" ItemsSource="{Binding Items}" /> 
</ScrollViewer> 

其中B:是包含此行为

触摸命名空间支持

要启用触摸支持,你可能还需要设置ScrollViewer.PanningModeNoneDataGrid和相同的属性设置为VerticalFirst或其他价值上的顶级ScrollViewer

<ScrollViewer VerticalScrollBarVisibility="Auto" Margin="5" PanningMode="VerticalFirst"> 
    <DataGrid ScrollViewer.PanningMode="None" ItemsSource="{Binding Items}" /> 
</ScrollViewer>