如何通过单击标题

时间:2015-05-06 09:39:12

标签: c# wpf sorting listview observablecollection

我是WPF的初学者。我试图通过单击列来排序(asc / desc)listview。我已将一个可观察的集合绑定到我的列表视图。我尝试了一些我在这里找到的东西https://msdn.microsoft.com/en-us/library/ms745786(v=vs.110).aspx由于某种原因,它对我不起作用。我的代码在此链接中几乎相同。但是无论如何我在这里写了它,虽然抱歉它有点长。

我做了一些搜索,很多回答已经说明我做了什么。所以我想我在某处犯了一些错误。

这是WPF:

    <ListView Grid.Row="3" Name="lvPurchaseSummaryList" ItemsSource="{Binding purchaseSummaryCol, Mode=Default}" Width="1700" HorizontalAlignment="Left" GridViewColumnHeader.Click="purchaseSummaryListColumn_click">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="100" DisplayMemberBinding="{Binding Path=originalName}" Header="Original Name" />
                <GridViewColumn Width="100" DisplayMemberBinding="{Binding Path=turkishName}" Header="Local Name"/>
 // List goes on like this...
            </GridView>
        </ListView.View>
</ListView>

这是类(与msdn链接几乎相同):

public partial class PurchaseSummaryPage : Page
{
    GridViewColumnHeader _lastHeaderClicked = null;
    ListSortDirection _lastDirection = ListSortDirection.Ascending;
    public PurchaseSummaryPage()
    {
        InitializeComponent();  
    }

    void purchaseSummaryListColumn_click(object sender, RoutedEventArgs e)
    {
        GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
        ListSortDirection direction;

        if (headerClicked != null)
        {
            if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
            {
                if (headerClicked != _lastHeaderClicked)
                {
                    direction = ListSortDirection.Ascending;
                }
                else
                {
                    if (_lastDirection == ListSortDirection.Ascending)
                    {
                        direction = ListSortDirection.Descending;
                    }
                    else
                    {
                        direction = ListSortDirection.Ascending;
                    }
                }

                string header = headerClicked.Column.Header as string;
                Sort(header, direction);

                if (direction == ListSortDirection.Ascending)
                {
                    headerClicked.Column.HeaderTemplate =
                      Resources["HeaderTemplateArrowUp"] as DataTemplate;
                }
                else
                {
                    headerClicked.Column.HeaderTemplate =
                      Resources["HeaderTemplateArrowDown"] as DataTemplate;
                }

                // Remove arrow from previously sorted header 
                if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
                {
                    _lastHeaderClicked.Column.HeaderTemplate = null;
                }


                _lastHeaderClicked = headerClicked;
                _lastDirection = direction;
            }
        }
    }


    private void Sort(string sortBy, ListSortDirection direction)
    {
        ICollectionView dataView =
          CollectionViewSource.GetDefaultView(lvPurchaseSummaryList.ItemsSource);

        dataView.SortDescriptions.Clear();
        SortDescription sd = new SortDescription(sortBy, direction);

        dataView.SortDescriptions.Add(sd);
        dataView.Refresh();
    }
}

这是来自主窗口的一些例子......

    public ObservableCollection<PurchaseSummary> purchaseSummaryCol { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        purchaseSummaryCol = new ObservableCollection<PurchaseSummary>();
        purchaseSummaryCol.Add(new PurchaseSummary("100", "basd", "asd", "basd", "asd", "basd", "asd", "basd", "asd", new Pay(DateTime.Today, DateTime.Today, 3), new PayRestrict(), new Vod()));
        purchaseSummaryCol.Add(new PurchaseSummary("105", "asd", "basd", "asd", "basd", "asd", "basd", "asd", "basd", new Pay(DateTime.Today, DateTime.Today, 3), new PayRestrict(), new Vod()));
        purchaseSummaryCol.Add(new PurchaseSummary("101", "casd", "cbasd", "casd", "cbasd", "casd", "cbasd", "casd", "cbasd", new Pay(DateTime.Today, DateTime.Today, 3), new PayRestrict(), new Vod()));

        PurchaseSummaryPage purchaseSummaryPage = new PurchaseSummaryPage();
        purchaseSummaryPage.DataContext = this;
        AttachPageToFrame(_f, purchaseSummaryPage);
    }

1 个答案:

答案 0 :(得分:1)

原因来自以下代码:

string header = headerClicked.Column.Header as string;
            Sort(header, direction);

它使用标题文本,在您的情况下,它与属性名称不同,简单的修复方法是尝试设置标题与属性名称Header="originalName"

相同

为了更好的解决方案,你可以存储propertyName需要在标题标签中排序,但我们需要做更多这样的事情:

 <GridViewColumn Width="100"  DisplayMemberBinding="{Binding Path=originalName}"  >
                    <GridViewColumn.Header>
                        <TextBlock Text="Original Name" Tag="originalName"/>
                    </GridViewColumn.Header>
                </GridViewColumn>

然后在代码中:

 GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
        var tb = headerClicked.Column.Header as TextBlock;
        var propertyName = tb.Tag as string;
        Sort(propertyName, direction);