0

我有一个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)       
}
4

1 回答 1

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>
于 2013-10-18T07:23:23.620 回答