如何调用“点击列标题"以编程方式进行活动? [英] How to call "click on column header" event programmatically?

查看:62
本文介绍了如何调用“点击列标题"以编程方式进行活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的问题有点复杂,但是我不知道如何解决,所以我尝试向您询问.
在WPF中,我的datagird列标题是可排序的.因此,当我单击一次时,将出现箭头,并且datagrid将按desc排序,然后按asc等排序.

我需要在窗口开始时进行排序(带有可见的箭头)的数据网格.所以我将SortDirection ="Descending"添加到我的DataGridTextColumn中.像这样:

Hi,
My problem is a little complex, but I have no idea how to solve it, so i try to ask you about this.
In WPF, my datagird column header is sortable. So, when i click once, the arrow appears and datagrid is sorted desc, then asc etc..

I need to have a sorted (with arrow visible) datagrid at start of my window. So I add SortDirection="Descending" to my DataGridTextColumn. Like this:

<DataGridTextColumn Binding="{Binding Path=SysTask}" Header="SysTask" Width="80" MinWidth="60" SortDirection="Descending"/>



现在,当我打开窗口时,datagrid已排序,箭头可见.但是..这还不行.它不是一样的,因为打开没有箭头的窗口,未排序的数据网格,然后单击标题进行排序.这是不一样的,我正在寻找事件-单击datagrid列标题进行排序".

我需要从代码中以编程方式单击列标题.有可能吗?

我告诉你为什么,这不一样,为什么我需要那个事件:

我有datagrid,必须每隔几秒钟对其进行排序. Datagrid可能很大,所以我这样做是这样的:获取新列表,检查是否有更改,然后删除或添加新记录.很简单..

但是,当我执行"SortDirection ="Descending"时,打开带有箭头等已排序数据网格的窗口,当我添加新记录时,它仍然出现在数据网格的末尾!然后,当我单击标题时-它移到了正确的位置.我只需要在列标题上单击一次,当方法将sth添加到列表时,它就在正确的位置.因此,我必须以编程方式单击此标题.

第二个原因,我在刷新时遇到问题.
当我从计时器调用刷新方法时:失败.当我通过按钮单击调用它时:它起作用.当计时器调用刷新方法时,它会失败,但是当我单击sort desc时,请单击标题上的sort asc-它可以工作.因此,我需要该单击列标题"事件.



Now, when I open my window, datagrid is sorted, the arrow is visible. But.. it''s not ok. It is NOT THE SAME, as open the window without arrow, not sorted datagrid, and then click the header to sort. It''s not the same, and i''m looking for the event - "click on the datagrid column header to sort".

I need, to click on column header programmatically from code. Is that possible?

I tell you why, it''s not the same and why I need that event:

I have datagrid, which has to be sorted every couple of seconds. Datagrid could be big, so I do it like that: get new list, check if there are changes and then remove or add new records. Very simple..

But, when I do "SortDirection="Descending"", open window with already sorted datagrid with arrow etc... and when I add new record, it still appears on the end of the datagrid! Then when I click on header - it''s moved for the right position. I have to click only once on column header, and when method add sth to list, it''s on the right place. So I have to click on this header programmatically.

A second reason, I have a problem with refreshing.
When I call my refresh method from timer: fail. When I call it from button click: it works. When refresh method is called by timer, it fails, but when I click sort desc, click sort asc on header - it works. So I need that "click column header" event for that to.

推荐答案

在对DataGrid进行排序时,最好的方法是将CollectionViewSortDescription一起使用.在XAML中,它看起来像这样:-

When it comes to sorting your DataGrid, the best thing is to use a CollectionView with a SortDescription. In XAML it would look something like this:-

<Grid.Resources>
            <CollectionViewSource x:Key="DefaultSort" Source="{Binding yourSourceHere}">
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="SysTask"/>
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </Grid.Resources>



(这假定您有一个映射到System.ComponentModel的名称空间scm)

然后您可以像这样设置网格:-



(this assumes you have a namespace scm mapped to System.ComponentModel)

Then you can set up your grid like this:-

<DataGrid ItemsSource="{Binding Source={StaticResource DefaultSort}}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=SysTask}" Header="SysTask" Width="80" SortDirection="Descending" />
            </DataGrid.Columns>
        </DataGrid>



然后,无论何时从网格中添加或减去项目,它都会根据该列Descending自动对所有内容进行排序.

希望对您有帮助



Then whenever you add or subtract items from the grid, it will automagically sort everything according to that column Descending.

Hope this helps


这篇关于如何调用“点击列标题"以编程方式进行活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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