none
wpf dataGrid滚动条 RRS feed

  • 问题

  • 当每秒向datagrid的数据源中增加一条记录的时候,怎么实现datagrid的滚动条实时的向下滚动? 谢谢
    2017年5月26日 11:10

全部回复

  • Hi lp9937,

    感谢你在MSDN论坛发帖,因为你的问题涉及到wpf问题,我将会把这个论坛移到wpf论坛。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年5月31日 1:42
  • Hi,

    尝试使用DataGrid.ScrollIntoView方法来让DataGrid一直滚动到数据的最底部。

    DataGrid.ScrollIntoView Method

    https://msdn.microsoft.com/en-us/library/cc673083(VS.95).aspx

    <Window x:Class="wpfAppDemo.wpfDataGrid.sample1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:wpfAppDemo.wpfDataGrid"
            mc:Ignorable="d"
            Title="sample1" Height="300" Width="600">
        <StackPanel>
            <ScrollViewer >
                <DataGrid x:Name="dataGrid" Margin="10" Width="300" Height="200" RowHeaderWidth="0"  ItemsSource="{Binding}" CanUserAddRows="False" AutoGenerateColumns="False" ColumnHeaderHeight="30" >
                    <DataGrid.ColumnHeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="DataGridColumnHeader">
                                        <Grid>
                                            <Border x:Name="HeaderBorder" BorderBrush="Black"  BorderThickness="0,0,0,1" Background="Azure"></Border>
                                            <ContentPresenter x:Name="HeaderContent"  RecognizesAccessKey="True" VerticalAlignment="Center" HorizontalAlignment="Center" />
                                            <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right"  Width="1" BorderThickness="1" BorderBrush="{Binding VerticalGridLinesBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" Cursor="SizeWE"/>
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGrid.ColumnHeaderStyle>
                    <DataGrid.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="DataGridCell">
                                        <Border x:Name="HeaderBorder" Background="Bisque" Padding="0" >
                                            <ContentPresenter x:Name="HeaderContent" Margin="0,0,0,1" RecognizesAccessKey="True"  VerticalAlignment="Center" HorizontalAlignment="Center" />
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGrid.CellStyle>
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" Width="1*"/>
                        <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" Width="1*"/>
                        <DataGridTextColumn Binding="{Binding Age}" Header="Age" Width="1*"/>
                    </DataGrid.Columns>
                </DataGrid>
            </ScrollViewer>
            <Button x:Name="addUser" Width="200" Height="30" Content="Add" Click="addUser_Click"/>
        </StackPanel>
    </Window>
    namespace wpfAppDemo.wpfDataGrid
    {
        /// <summary>
        /// Interaction logic for sample1.xaml
        /// </summary>
        public partial class sample1 : Window
        {
    
            ObservableCollection<User> userList = new ObservableCollection<User>() {
                new User() {FirstName="Bob",LastName="Ding",Age=27 },
                new User() {FirstName="Wendy",LastName="Zang",Age=24 },
                new User() {FirstName="Emily",LastName="Zhang",Age=24 },
                new User() {FirstName="Stanly",LastName="Fan",Age=25 },
                new User() {FirstName="Hart",LastName="Wang",Age=27 },
                new User() {FirstName="Amol",LastName="Wang",Age=32 },
                new User() {FirstName="kobe",LastName="Brant",Age=39 },
            };
            public sample1()
            {
                InitializeComponent();
                this.DataContext = userList;
                dataGrid.ScrollIntoView(userList.Last());
            }
            static int index = 0;
            private void addUser_Click(object sender, RoutedEventArgs e)
            {
                userList.Add(new User { FirstName = "first name"+ index, LastName = "last name"+index, Age = 39 });
                dataGrid.ScrollIntoView(userList.Last());
                index++;
            }
        }
        public class User
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Age { get; set; }
        }
    }


    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年5月31日 2:06
    版主