2013-10-16 85 views
0

我有DataGrid並已實現拖放功能 現在,當我嘗試滾動時,它不工作只是顯示裸體標誌。 我訂閱ScrollBar.Scroll,但它裏面,我不能得到的信息如何滾動, 我不知道我是否應該向上或向下滾動。我一定錯過了什麼。請幫助。謝謝DataGrid無法滾動B/C拖放

<toolkit:DataGrid x:Name="dg"                 
    Style="{StaticResource DataGridStyle}" 
    RowDetailsVisibilityMode="VisibleWhenSelected" 
    ItemsSource="{Binding Relations}"       
    SelectionUnit="FullRow" 
    SelectedItem="{Binding ListSelection}" IsReadOnly="True" 
    VerticalScrollBarVisibility="Auto" 
    HorizontalScrollBarVisibility="Auto" 
    MouseDoubleClick="dg_MouseDoubleClick" 
    PreviewKeyDown="DgPreviewKeyDown" 
    PreviewMouseWheel="DgPreviewMouseWheel" 
    PreviewMouseLeftButtonDown="DataGridPreviewMouseLeftButtonDown" 
    PreviewMouseMove="DataGridMouseMove" 
    Drop="DataGridDrop" 
    DragEnter="DataGridDragEnter" 
    ScrollBar.Scroll="DgScroll"> 
    <toolkit:DataGrid.Columns> 
     <toolkit:DataGridTextColumn Header="Symbol" Binding="{Binding Relation}" MinWidth="310"> 
      <toolkit:DataGridTextColumn.ElementStyle> 
       <Style TargetType="{x:Type TextBlock}"> 
        <Setter Property="TextWrapping" Value="Wrap" /> 
       </Style> 
      </toolkit:DataGridTextColumn.ElementStyle> 
     </toolkit:DataGridTextColumn> 
     <toolkit:DataGridTextColumn Header="Description" Width="*" MinWidth="300" Binding="{Binding Description}"> 
      <toolkit:DataGridTextColumn.ElementStyle> 
       <Style TargetType="{x:Type TextBlock}"> 
        <Setter Property="TextWrapping" Value="Wrap" /> 
       </Style> 
      </toolkit:DataGridTextColumn.ElementStyle> 
     </toolkit:DataGridTextColumn> 
    </toolkit:DataGrid.Columns> 
</toolkit:DataGrid> 

private void DgScroll(object sender, ScrollEventArgs e) 
{ 
    var viewer = DragDropHelper.GetDescendantByType(dg, typeof(ScrollViewer)) as ScrollViewer; 
    if (viewer == null) return; 

    var newVal = e.NewValue; //How I can know scroll up or down??? 
    viewer.ScrollToVerticalOffset(viewer.VerticalOffset + 30)  
} 
+0

我沒有找到http://stackoverflow.com/questions/4124877/wpf-datagrid-cant-use-mouse-to-scroll-because-of-drag-drop – toosensitive

+0

我一個解決方案嘗試了一個單獨的scrollviewer,但這會導致新的問題 - 性能下降。 http://stackoverflow.com/questions/11127511/slow-performance-with-wpf-datagrid-and-scrollviewer?rq=1 – toosensitive

+0

看看這個問題的答案:http://stackoverflow.com/questions/ 10733581 /滾動-而拖動和墜-WPF –

回答

1

好的,你可以使用風格來解決你的問題。 EventHandler OnRepeatButtonClicked嘗試獲取可視父項(滾動條)並存儲scoll的當前值。 並且在ValueChangedEventHandler中,您可以檢查新滾動值是增加(向下)還是減少(向上)。 每次滾動時都要更新當前值。 參考

<Style x:Key="VerticalScrollBarStyle" TargetType="ScrollBar"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ScrollBar"> 
        <Grid x:Name="VerticalRoot"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="*" /> 
          <RowDefinition Height="Auto" /> 
         </Grid.RowDefinitions> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Disabled"/> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Rectangle RadiusX="8" RadiusY="9" Stroke="#0C848D8D" Grid.Row="1" Grid.RowSpan="3" > 
          <Rectangle.Fill> 
           <LinearGradientBrush EndPoint="0.5,0.5" StartPoint="0,0.5"> 
            <GradientStop Color="#33000000" Offset="0.061"/> 
            <GradientStop Color="#19EEFEFF" Offset="0.788"/> 
           </LinearGradientBrush> 
          </Rectangle.Fill> 
         </Rectangle> 
         <RepeatButton x:Name="VerticalLargeDecrease" Opacity="0" Grid.Row="1" Click="OnRepeatButtonClicked"/> 
         <Thumb x:Name="VerticalThumb" Grid.Row="2" VerticalAlignment="Center" Style="{StaticResource VerticalScrollbarThumbStyle}"/> 
         <RepeatButton x:Name="VerticalLargeIncrease" Opacity="0" Grid.Row="3" Click="OnRepeatButtonClicked"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Orientation" Value="Vertical"/> 
    </Style>