.net 单击标题时对列表视图排序
我需要根据单击ListView标题对ListView上的内容进行排序。我使用的是C++/CLI,所以我得到了代码,并使用C++/CLI对其进行了编码 以下是我的结果: ListViewColumnSorter.h.net 单击标题时对列表视图排序,.net,windows,listview,c++-cli,clr,.net,Windows,Listview,C++ Cli,Clr,我需要根据单击ListView标题对ListView上的内容进行排序。我使用的是C++/CLI,所以我得到了代码,并使用C++/CLI对其进行了编码 以下是我的结果: ListViewColumnSorter.h /// <summary> /// This class is an implementation of the 'IComparer' interface. /// </summary> #pragma once public ref class ListV
/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
#pragma once
public ref class ListViewColumnSorter : System::Collections::IComparer
{
public:
ListViewColumnSorter();
virtual int Compare(System::Object^ x, System::Object^ y);
private:
/// <summary>
/// Specifies the column to be sorted
/// </summary>
int ColumnToSort;
/// <summary>
/// Specifies the order in which to sort (i.e. 'Ascending').
/// </summary>
System::Windows::Forms::SortOrder OrderOfSort;
/// <summary>
/// Case insensitive comparer object
/// </summary>
System::Collections::CaseInsensitiveComparer^ ObjectCompare;
property int SortColumn;
property System::Windows::Forms::SortOrder SortOrder;
};
//
///此类是“IComparer”接口的实现。
///
#布拉格语一次
公共引用类ListViewColumnSorter:System::Collections::IComparer
{
公众:
ListViewColumnSorter();
虚拟整数比较(系统::对象^x,系统::对象^y);
私人:
///
///指定要排序的列
///
int-ColumnToSort;
///
///指定排序的顺序(即“升序”)。
///
系统::Windows::窗体::排序器排序顺序;
///
///不区分大小写的比较器对象
///
系统::集合::不区分大小写比较^ObjectCompare;
属性int SortColumn;
属性系统::Windows::窗体::排序器排序器;
};
ListViewColumnSorter.cpp
/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
#include "ListViewColumnSorter.h"
ListViewColumnSorter::ListViewColumnSorter()
{
// Initialize the column to '0'
ColumnToSort = 0;
// Initialize the sort order to 'none'
OrderOfSort = System::Windows::Forms::SortOrder::None;
// Initialize the CaseInsensitiveComparer object
ObjectCompare = gcnew System::Collections::CaseInsensitiveComparer();
}
/// <summary>
/// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison.
/// </summary>
/// <param name="x">First object to be compared</param>
/// <param name="y">Second object to be compared</param>
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
int ListViewColumnSorter::Compare(System::Object^ x, System::Object^ y)
{
int compareResult;
System::Windows::Forms::ListViewItem^ listviewX, listviewY;
// Cast the objects to be compared to ListViewItem objects
listviewX = (System::Windows::Forms::ListViewItem^) x; <--- ERROR HERE
listviewY = (System::Windows::Forms::ListViewItem^) y; <--- ERROR HERE
// Compare the two items
compareResult = ObjectCompare->Compare(listviewX->SubItems[ColumnToSort].Text,
listviewY->SubItems[ColumnToSort].Text);
// Calculate correct return value based on object comparison
if (OrderOfSort == System::Windows::Forms:: SortOrder::Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == System::Windows::Forms::SortOrder::Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
}
/// <summary>
/// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
/// </summary>
void ListViewColumnSorter::SortColumn::set(int value)
{
ColumnToSort = value;
}
int ListViewColumnSorter::SortColumn::get()
{
return ColumnToSort;
}
/// <summary>
/// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
/// </summary>
void ListViewColumnSorter::SortOrder::set(System::Windows::Forms::SortOrder value)
{
OrderOfSort = value;
}
System::Windows::Forms::SortOrder ListViewColumnSorter::SortOrder::get()
{
return OrderOfSort;
}
//
///此类是“IComparer”接口的实现。
///
#包括“ListViewColumnSorter.h”
ListViewColumnSorter::ListViewColumnSorter()
{
//将列初始化为“0”
ColumnToSort=0;
//将排序顺序初始化为“无”
OrderOfSort=System::Windows::Forms::SortOrder::None;
//初始化CaseInsensitiveComparer对象
ObjectCompare=gcnew System::Collections::CaseInsensitiveComparer();
}
///
///此方法继承自IComparer接口。它使用不区分大小写的比较来比较传递的两个对象。
///
///第一个要比较的对象
///第二个要比较的对象
///比较的结果。如果相等,则为“0”;如果“x”小于“y”,则为负值;如果“x”大于“y”,则为正值
int ListViewColumnSorter::Compare(系统::对象^x,系统::对象^y)
{
int比较结果;
系统::Windows::窗体::ListViewItem ^listviewX,listviewY;
//强制转换要与ListViewItem对象进行比较的对象
listviewX=(System::Windows::Forms::ListViewItem^)x;子项[ColumntSort]。文本,
listviewY->子项[ColumntSort].Text);
//基于对象比较计算正确的返回值
if(OrderOfSort==System::Windows::Forms::SortOrder::Ascending)
{
//选择升序排序,返回比较操作的正常结果
返回比较结果;
}
else if(OrderOfSort==System::Windows::Forms::SortOrder::Descending)
{
//选择降序排序,则返回比较操作的负结果
返回(-compareResult);
}
其他的
{
//返回“0”以指示它们相等
返回0;
}
}
///
///获取或设置要应用排序操作的列的编号(默认为“0”)。
///
void ListViewColumnSorter::SortColumn::set(int值)
{
ColumnToSort=值;
}
int ListViewColumnSorter::SortColumn::get()
{
返回列排序;
}
///
///获取或设置要应用的排序顺序(例如,“升序”或“降序”)。
///
void ListViewColumnSorter::SortOrder::set(系统::Windows::窗体::SortOrder值)
{
排序顺序=值;
}
System::Windows::Forms::SortOrder ListViewColumnSorter::SortOrder::get()
{
排序的返回顺序;
}
我在编译此代码时遇到问题,在函数比较中,我无法将ListViewItem^转换为listviewX和listviewY以获取其字符串(检查代码标记):
表达式必须具有类类型
解决这个问题需要一些建议 您是否正在寻找从
Object^
转换为ListViewItem^
的正确方法
转换为托管类型的常用方法是使用动态转换
或静态转换
我会使用dynamic\u cast(x)
,但这确实需要您检查nullptr
。您还可以使用static\u cast(x)
,如果类型不正确,它将抛出异常
另外,请注意,没有什么可以阻止您在C#中实现
ListViewColumnSorter
类,并从C++/CLI中引用该类。谢谢您的cast提示。我现在面临另一个问题:listviewY=static\u cast(y)代码>。错误消息=No operator=与这些操作数匹配。System::Windows::Forms::ListViewItem=System::Windows::Forms::ListViewItem^
。ListViewItem
从何而来,因为它们都是ListViewItem^
?困惑现在…发现了问题。不能在同一行中声明两个变量,如:System::Windows::Forms::ListViewItem^listviewX,listviewY代码>。添加了2条语句,现在一切正常。谢谢你帮助大卫Yaw@Mendes:您可以,但只有基类型适用于这两种类型,您有一个ListViewItem^
类型的变量和一个ListViewItem
类型的变量。如果需要两个ListViewItem^
,请重复修改器,即ListViewItem ^x,^y代码>