我工作的一个一个排序WPF ListView 我做了很多的进展。这并不难因为有很多东西在互联网上。但是有一点点的信息,我仍然失踪。

列如下:

<GridViewColumn Width="200" Header="Fahrzeugname" DisplayMemberBinding="{Binding Name}">

我可以整理这样的:

Binding columnBinding = column.DisplayMemberBinding as Binding;

if (columnBinding != null)
{
    sorts.Clear();
    sorts.Add(new SortDescription(columnBinding.Path.Path, direction));
    lastColumnSorted = column;
}

但是我的问题是我没有 DisplayMemberBinding 因为我用一个 DataTemplate:

<DataTemplate>
    <TextBlock Text="{Binding Name}" TextAlignment="Left"/>
</DataTemplate>

我怎么得到的 Binding 属于此列在C#代码?

有帮助吗?

解决方案

我写了一组附加属性到底该怎么做肯特建议,你可以检查出来的此处


编辑:根据要求,这里的命令的一个样本为GridViewSort.Command

    private ICommand _sortCommand;
    public ICommand SortCommand
    {
        get
        {
            if (_sortCommand == null)
            {
                _sortCommand = new RelayCommand(SortPersonsBy);
            }
            return _sortCommand;
        }
        set { _sortCommand = value; }
    }

    private void SortPersonsBy(object param)
    {
        string propertyName = param as string;
        ICollectionView view = CollectionViewSource.GetDefaultView(_persons);
        ListSortDirection direction = ListSortDirection.Ascending;
        if (view.SortDescriptions.Count > 0)
        {
            SortDescription currentSort = view.SortDescriptions[0];
            if (currentSort.PropertyName == propertyName)
            {
                if (currentSort.Direction == ListSortDirection.Ascending)
                    direction = ListSortDirection.Descending;
                else
                    direction = ListSortDirection.Ascending;
            }
            view.SortDescriptions.Clear();
        }
        if (!string.IsNullOrEmpty(propertyName))
        {
            view.SortDescriptions.Add(new SortDescription(propertyName, direction));
        }
    }

(它实际上实现了相同的行为时GridViewSort.AutoSort被设置为true为...)

其他提示

的WPF内置的方式将是使用的ICollectionView的。

ICollectionView view = CollectionViewSource.GetDefaultView(yourItemsSourceList);
if(view.CanSort)
{
   view.SortDescriptions.Add(...)
}

为什么你会尝试自动做到这一点?为什么不提供附加属性,用户可以设置来指示通过该柱应该排序的属性

<GridViewColumn s:SortableColumn.SortBy="{Binding Name}" ...>

这样您的实施提供更多的自由,并且代码会更快,更可靠。

我面临一个类似的WPF列表视图/内的排序问题,我有一些约束,这使得它难以实现C#解决方案提供。基本上我有一个现有的ASP.NET 应用程序与数千行的调试VB代码,以及工作SQL数据库,有超过100表和一些非常复杂的加入。我必须要写一个窗口。exe基于相同的应用程序,但我想使用WPF实现一个类似的图形的外观和感觉。

具有完成免责声明,我遇到的排序问题和搜索互联网上所有伟大的共同概念,如上述,并试图将它们从CS入VB。我是有经验的程序员,但我发现,这只是超过我所以我把elsewhwere和开发我自己进行排序,而不是。它可能会吓坏的纯粹主义者但我是个实用主义者和制造商业应用程序的优先。

为了排序的网格,我决定只是用鼠标上的事件所列标题。双击上升种和MouseRightButtonUp为下降。

<GridViewColumn Header="Project Name &#160;&#160;" Width="300" >

Private Sub ListViewGrid_MouseDoubleClick(
    ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles ListingDG.MouseDoubleClick

    Try
        Dim mouseHdrClick As String = DirectCast(DirectCast(e.OriginalSource, System.Object), System.Windows.Controls.TextBlock).Text
        Dim SqlOrderBy As String = ""

        Select Case UCase(mouseHdrClick.Trim)
            Case UCase("Product Name")
                SqlOrderBy = " ORDER BY Product_Name"

然后,我刚刚重新装载ListViewGrid使用SqlOrderBy串的变量。唯一的问题,我遇到的是我迫击在标题中的文字列标题对它的工作。我得到解决,只需追加XML空间字(&#160;)填充柱的宽度。的 .Trim 命令除掉他们再次选择的情况下工作。我还没有想出如何加点向上和向下箭但功能是重要的,因为这些仅仅是化妆品接触的权利。

<GridViewColumn Header="Project Name &#160;&#160;" Width="300">

最后,让我补充说,我充分意识到,如果我改变结构内它将会导致头文本的移动的其他地方OriginalSource但是,这是一个问题,我可以住在一起。我知道,这是一个快速和肮脏的解决办法和可能有更好的替代品,因此任何建议将不胜感激。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top