DataGrid无法滚动b / c拖放

时间:2013-10-16 21:08:28

标签: c# wpf datagrid scrollbar

我有一个DataGrid并且已经实现了拖放功能 现在,当我尝试滚动时,它不起作用只是显示一个裸体标志。 我订阅ScrollBar.Scroll,但在其中,我无法获取如何滚动的信息, 我不知道我是应该向上或向下滚动。我一定错过了什么。请帮忙。谢谢

<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)       
}

1 个答案:

答案 0 :(得分: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>