none
wpf 如何控制datagrid中的滚动条 RRS feed

  • 问题

  • 在wpf中 怎么后台控制datagrid的滚动条,鼠标按下后上下移动可以使datagrid中的数据上下滚动
    2014年6月16日 8:14

答案

  • 尝试过了,但是滑的时候没那种平滑效果,一下子就划过去了,没有像手机一样慢慢的滑

    Xmal代码

     <ScrollViewer x:Name="scroll" VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="False">
                       <DataGrid x:Name="DataFoodList" ItemsSource="{Binding}" 
                              SelectionUnit="FullRow" AutoGenerateColumns="False" 
                              AlternatingRowBackground="#00F7F7F7" IsReadOnly="True" 
                              HorizontalGridLinesBrush="{x:Null}" VerticalGridLinesBrush="{x:Null}" 
                              SelectedIndex="0" ScrollViewer.CanContentScroll="False" 
                              VerticalScrollBarVisibility="Hidden" RowDetailsVisibilityMode="VisibleWhenSelected"  
                              BorderThickness="0" RowHeaderWidth="0" RenderTransformOrigin="0.5,0.5" 
                              SelectionMode="Single" Margin="0,-6,0,6" 
                              MouseLeftButtonDown="DataFoodList_MouseLeftButtonDown"
                              MouseLeftButtonUp="DataFoodList_MouseLeftButtonUp" ForceCursor="False" Focusable="True">
                        <DataGrid.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </DataGrid.RenderTransform>
                        <DataGrid.RowStyle>
                            <Style TargetType="{x:Type DataGridRow}">
                                <Style.Triggers>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter Property="BorderThickness" Value="0"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.RowStyle>
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="Id" Header="序号" Width="*" FontSize="16" Binding="{Binding Id}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTextColumn x:Name="FoodName" Header="菜名" Width="*" FontSize="16" Binding="{Binding FoodName}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTextColumn x:Name="TableId" Header="桌位" Width="*" FontSize="16" Binding="{Binding TableId}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTextColumn x:Name="Time" Header="时间" Width="*" FontSize="16" Binding="{Binding Time, StringFormat=yyyy-MM-dd-hh:mm}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTemplateColumn x:Name="Exit" Header="操作" Width="*" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button x:Name="button" Content="  传 菜" Height="36" Margin="0,5" Style="{DynamicResource ButtonStyle1}" Width="80" Click="button_Click"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                  </ScrollViewer>
                

    后台代码

    Point StartPoint; //开始
            Point EndPoint;   //结束
            double x;
            double y;
            private void DataFoodList_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                StartPoint = PointToScreen(Mouse.GetPosition(this));
            }
            private void DataFoodList_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                ScrollViewer scrollbar = new ScrollViewer();
                EndPoint = PointToScreen(Mouse.GetPosition(this));
                x = EndPoint.X - StartPoint.X;
                y = EndPoint.Y - StartPoint.Y;
      if (tabControl1.SelectedIndex == 0)
                {
                    if (y > 0)
                    {
                        for (int i = 0; i < y / 30; i++)
                        {
                            scroll.LineUp();
                        }
                    }
                    else if (y < 0)
                    {
                        for (int i = 0; i < (-y) / 30; i++)
                        {
                            scroll.LineDown();
                        }
                    }
                }
                if (tabControl1.SelectedIndex == 1)
                {
                    if (y > 0)
                    {
                        for (int i = 0; i < y / 30; i++)
                        {
                            scroll2.LineUp();
                        }
                    }
                    else if (y < 0)
                    {
                        for (int i = 0; i < (-y) / 30; i++)
                        {
                            scroll2.LineDown();
                        }
                    }
                }
                else
                {
                    return;
                }
            }


    2014年6月17日 6:16

全部回复

  • 你好,

    如果需要DataGrid中有滚动条,可以采用下面这种方式:
    XAML:

            <ScrollViewer HorizontalScrollBarVisibility="Auto">
                <DataGrid ItemsSource="{Binding Path=.}" 
                            AutoGenerateColumns="False">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Id:" Binding="{Binding Path=Id}" Width="200" />
                        <DataGridTextColumn Header="Name:" Binding="{Binding Path=Name}" Width="200" />
                        <DataGridTextColumn Header="Description:" Binding="{Binding Path=Description}" Width="400"/>
                    </DataGrid.Columns>
                </DataGrid>
            </ScrollViewer>
    有滚动条的时候我们就可以通过鼠标进行操作了。不需要额外的代码来控制。

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月17日 2:07
    版主
  • 谢谢 但是我要的不是这个效果,我想要的是像手机上那样的平滑效果,用鼠标按下后的移动距离控制数据滚动
    2014年6月17日 4:01
  • 这个貌似做不到吧。你做过一些尝试吗?或者代码之类的。
    2014年6月17日 4:52
  • 尝试过了,但是滑的时候没那种平滑效果,一下子就划过去了,没有像手机一样慢慢的滑

    Xmal代码

     <ScrollViewer x:Name="scroll" VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="False">
                       <DataGrid x:Name="DataFoodList" ItemsSource="{Binding}" 
                              SelectionUnit="FullRow" AutoGenerateColumns="False" 
                              AlternatingRowBackground="#00F7F7F7" IsReadOnly="True" 
                              HorizontalGridLinesBrush="{x:Null}" VerticalGridLinesBrush="{x:Null}" 
                              SelectedIndex="0" ScrollViewer.CanContentScroll="False" 
                              VerticalScrollBarVisibility="Hidden" RowDetailsVisibilityMode="VisibleWhenSelected"  
                              BorderThickness="0" RowHeaderWidth="0" RenderTransformOrigin="0.5,0.5" 
                              SelectionMode="Single" Margin="0,-6,0,6" 
                              MouseLeftButtonDown="DataFoodList_MouseLeftButtonDown"
                              MouseLeftButtonUp="DataFoodList_MouseLeftButtonUp" ForceCursor="False" Focusable="True">
                        <DataGrid.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </DataGrid.RenderTransform>
                        <DataGrid.RowStyle>
                            <Style TargetType="{x:Type DataGridRow}">
                                <Style.Triggers>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter Property="BorderThickness" Value="0"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.RowStyle>
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="Id" Header="序号" Width="*" FontSize="16" Binding="{Binding Id}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTextColumn x:Name="FoodName" Header="菜名" Width="*" FontSize="16" Binding="{Binding FoodName}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTextColumn x:Name="TableId" Header="桌位" Width="*" FontSize="16" Binding="{Binding TableId}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTextColumn x:Name="Time" Header="时间" Width="*" FontSize="16" Binding="{Binding Time, StringFormat=yyyy-MM-dd-hh:mm}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" />
                            <DataGridTemplateColumn x:Name="Exit" Header="操作" Width="*" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button x:Name="button" Content="  传 菜" Height="36" Margin="0,5" Style="{DynamicResource ButtonStyle1}" Width="80" Click="button_Click"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                  </ScrollViewer>
                

    后台代码

    Point StartPoint; //开始
            Point EndPoint;   //结束
            double x;
            double y;
            private void DataFoodList_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                StartPoint = PointToScreen(Mouse.GetPosition(this));
            }
            private void DataFoodList_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                ScrollViewer scrollbar = new ScrollViewer();
                EndPoint = PointToScreen(Mouse.GetPosition(this));
                x = EndPoint.X - StartPoint.X;
                y = EndPoint.Y - StartPoint.Y;
      if (tabControl1.SelectedIndex == 0)
                {
                    if (y > 0)
                    {
                        for (int i = 0; i < y / 30; i++)
                        {
                            scroll.LineUp();
                        }
                    }
                    else if (y < 0)
                    {
                        for (int i = 0; i < (-y) / 30; i++)
                        {
                            scroll.LineDown();
                        }
                    }
                }
                if (tabControl1.SelectedIndex == 1)
                {
                    if (y > 0)
                    {
                        for (int i = 0; i < y / 30; i++)
                        {
                            scroll2.LineUp();
                        }
                    }
                    else if (y < 0)
                    {
                        for (int i = 0; i < (-y) / 30; i++)
                        {
                            scroll2.LineDown();
                        }
                    }
                }
                else
                {
                    return;
                }
            }


    2014年6月17日 6:16