Windows Presentation Foundation
.NET Framework 的一部分,它提供统一的编程模型,用于在 Windows 上构建业务线桌面应用程序。
128 个问题
源线程:如何使用不同的 dataTemplate (int, bool, string, ...) 对列表视图进行排序,由 Alex Li-MSFT 回答。
我有一个 ListView 控件,当用户单击特定列标题时,我想对列表中的所有项目进行排序,如何实现它?
这是我的 XAML 代码:
<ListView ItemsSource="{Binding}" x:Name="listViewControl">
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SomeInteger}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SomeString}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding SomeBool}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Category" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ElementName=mainWindow, Path=StringList}"
SelectedItem="{Binding Path=SomeString}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView>
Note:此问题总结整理于:How to sort ListView by selected column?
你好
欢迎来到我们的 Microsoft 问答平台!
您可以使用我的演示:
public partial class MainWindow : Window
{
ObservableCollection<MyClass> list = new ObservableCollection<MyClass>();
public MainWindow()
{
InitializeComponent();
list.Add(new MyClass() { SomeBool = false, SomeInteger = 1, SomeString = "3" });
list.Add(new MyClass() { SomeBool = false, SomeInteger = 2, SomeString = "2" });
list.Add(new MyClass() { SomeBool = true, SomeInteger = 3, SomeString = "3" });
this.DataContext = list;
}
private void ListViewControl_Click(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is GridViewColumnHeader)
{
GridViewColumn clickedColumn = (e.OriginalSource as GridViewColumnHeader).Column;
string bindingProperty=null;
if (clickedColumn != null)
{
if (clickedColumn.Header.ToString() == "column1")
{
bindingProperty = "SomeInteger";
}
if (clickedColumn.Header.ToString() == "column2")
{
bindingProperty = "SomeString";
}
if (clickedColumn.Header.ToString() == "column3")
{
bindingProperty = "SomeBool";
}
SortDescriptionCollection sdc = listViewControl.Items.SortDescriptions;
ListSortDirection sortDirection = ListSortDirection.Ascending;
if (sdc.Count > 0)
{
SortDescription sd = sdc[0];
sortDirection = (ListSortDirection)((((int)sd.Direction) + 1) % 2);
sdc.Clear();
}
sdc.Add(new SortDescription(bindingProperty, sortDirection));
}
}
}
}
public class MyClass
{
public int SomeInteger { get; set; }
public string SomeString { get; set; }
public bool SomeBool { get; set; }
}
xaml:
<Grid>
<ListView ItemsSource="{Binding}" x:Name="listViewControl" GridViewColumnHeader.Click="ListViewControl_Click">
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn Header="column1" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SomeInteger}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="column2">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SomeString}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="column3">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding SomeBool}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Category" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ElementName=mainWindow, Path=StringList}"
SelectedItem="{Binding Path=SomeString}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
谢谢。
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。