在WPF中重新加载DataGrid之后如何保存位置 [英] How to save position after reload DataGrid in WPF

查看:57
本文介绍了在WPF中重新加载DataGrid之后如何保存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说的是DataGrid 不是DataGridView
我在这里正在刷新包含< DataGrid.GroupStyle> 的数据网格,并且我正在显示按顺序对数据库进行分组的数据,并且我想做的就是刷新我的数据网格。

I'm talking about DataGrid not DataGridView! What I'm doing here is refreshing datagrid which contains <DataGrid.GroupStyle> and I'm showing my data from database grouped by number of order, and what I want to do is refresh my datagrid.

这是我的代码:

 public MainWindow()
    {
        try
        {


            InitializeComponent();


            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.WindowState = WindowState.Maximized;

            var ordersList = OrdersController.localOrders();


            collectionViewSource.Source = ordersList;
            collectionViewSource.GroupDescriptions.Add(new PropertyGroupDescription("NumberOfOrder"));
            DataContext = collectionViewSource;

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(8);
            timer.Tick += timer_Tick;
            timer.Start();

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    void timer_Tick(object sender, EventArgs e)
    {

        var ordersList = OrdersController.localOrders();
        collectionViewSource.Source = null;
        collectionViewSource.Source = ordersList;

        DataContext = collectionViewSource;

        datagrid1.ScrollIntoView(datagrid1.Items[datagrid1.Items.Count - 1]);
    }
}

XAML:

<DataGrid  HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden"  BorderBrush="#83D744" IsSynchronizedWithCurrentItem="False" VerticalGridLinesBrush="Transparent" Grid.Column="0"   RowHeaderWidth="0" CanUserAddRows="False" AutoGenerateColumns="False"  x:Name="datagrid1" Margin="10,150,8,50" Background="Transparent" RowBackground="#FF494949"  VerticalContentAlignment="Center" HorizontalContentAlignment="Center" ItemsSource="{Binding}">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Background" Value="#83D744"/>
                <Setter Property="Opacity" Value="1"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="FontSize" Value="18"/>
                <Setter Property="FontFamily" Value="Arial"/>
                <Setter Property="Height" Value="50"/>
            </Style>


    <Style x:Key="TextInCellCenter" TargetType="{x:Type TextBlock}" >
        <Setter Property="TextAlignment" Value="Center"/>
    </Style>
    <Style TargetType="{x:Type TextBlock}" x:Key="RightAligElementStyle">
        <Setter Property="TextAlignment" Value="Right"/>
    </Style>
    <Style TargetType="{x:Type TextBlock}" x:Key="LeftAligElementStyle">
        <Setter Property="TextAlignment" Value="Left"/>
    </Style>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
               Color="Transparent"/>
    </DataGrid.Resources>


    <DataGrid.Columns >
        <DataGridTextColumn Binding="{Binding ProductName}"     ElementStyle="{StaticResource LeftAligElementStyle}"     Header="NAZIV ARTIKLA" MinWidth="350"    Foreground="White" FontSize="20" FontFamily="Verdana" />
        <DataGridTextColumn Binding="{Binding Quantity}"        ElementStyle="{StaticResource TextInCellCenter}"         Header="KOLIČINA"   MinWidth="200" Foreground="White"      FontSize="20" FontFamily="Verdana" />
    </DataGrid.Columns>

    <DataGrid.GroupStyle>
        <!-- Style for groups at top level. -->
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander IsExpanded="True"  Background="Black" Opacity="0.7">
                                    <Expander.Header >
                                        <DockPanel Height="50" Margin="0,0,0,0"  Name="dockPanel" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=ActualWidth}">

                                            <Button Name="btnFinishOrder" Content="Finish Order" Margin="0,0,55,5" DockPanel.Dock="Right" Click="btnFinishOrder_Click" FontSize="12" BorderThickness="1.5" HorizontalAlignment="Left"  VerticalAlignment="Bottom"  HorizontalContentAlignment="Center" VerticalContentAlignment="Center"  Foreground="#83D744"  Background="Transparent"  BorderBrush="#83D744" Width="130"   Height="40">
                                                 <Button.Template>
                                                    <ControlTemplate TargetType="Button">
                                                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                                                            BorderBrush=    "{TemplateBinding BorderBrush}"
                                                            Background=     "{TemplateBinding Background}">
                                                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                    </Border>
                                                    </ControlTemplate>
                                                 </Button.Template>
                                             </Button>

                                             <Button Name="btnTakeIt" Click="btnTakeIt_Click"  Content="Take it" Margin="0,0,20,5" DockPanel.Dock="Right" FontSize="12" BorderThickness="1.5" HorizontalAlignment="Left"  VerticalAlignment="Bottom"  HorizontalContentAlignment="Center" VerticalContentAlignment="Center"  Foreground="#83D744"  Background="Transparent"  BorderBrush="#83D744" Width="130"   Height="40">
                                                 <Button.Template>
                                                    <ControlTemplate TargetType="Button">
                                                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                                                            BorderBrush="{TemplateBinding BorderBrush}"
                                                            Background="{TemplateBinding Background}">
                                                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                    </Border>
                                                    </ControlTemplate>
                                                 </Button.Template>
                                            </Button>
                                            <TextBlock FontWeight="Normal" FontFamily="Verdana" FontSize="20" Height="25" Foreground="#83D744" Text="{Binding Path=Name,StringFormat= Number of Order:# {0}}" />

                                        </DockPanel>
                                    </Expander.Header>
                                    <Expander.Content>
                                        <ItemsPresenter />
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

重新加载数据后,单元格选择会跳到第一列,并重置滚动条。如何保存DataGrid的位置?

after reload data, cell selection jumps on first column and scrollbars is reset. How to save position of DataGrid?

推荐答案

如果需要滚动到特定项目,只需保存索引并使用:

If you need to scroll to specific item, just save the index and use:

dataGrid.ScrollIntoView(dataGrid.Items[itemIndex]);

您可以使用它滚动到DataGrid中的最后一项:

You can use this to scroll to the last item in the DataGrid:

dataGrid.ScrollIntoView(dataGrid.Items[dataGrid.Items.Count - 1]);

编辑:

使用 CollectionViewSource 进行分组时, DataGrid 滚动似乎存在问题。

There seems to be a problem with DataGrid scrolling when using CollectionViewSource and grouping.

我设法使它以其他方式工作...而不是

I managed to get it to work the other way... Instead of

dataGrid.ScrollIntoView(dataGrid.Items[dataGrid.Items.Count - 1])

尝试使用以下代码进行滚动:

try using the following code to do the scrolling:

if (datagrid1.Items.Count > 0)
{
    var border = VisualTreeHelper.GetChild(datagrid1, 0) as Decorator;
    if (border != null)
    {
        var scrollViewer = border.Child as ScrollViewer;
        if (scrollViewer != null) scrollViewer.ScrollToEnd();
    }
}

这篇关于在WPF中重新加载DataGrid之后如何保存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆