大衛布朗鏈接中的想法是使用一個工作的價值轉換器。以下是一個完整的工作示例。列表框具有行號,可以按姓名和年齡進行排序。
XAML:
<Window x:Class="NumberedListBox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NumberedListBox"
Height="300" Width="300">
<Window.Resources>
<local:RowNumberConverter x:Key="RowNumberConverter" />
<CollectionViewSource x:Key="sortedPersonList" Source="{Binding Path=Persons}" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
ItemsSource="{Binding Source={StaticResource sortedPersonList}}"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{Binding Converter={StaticResource RowNumberConverter}, ConverterParameter={StaticResource sortedPersonList}}"
Margin="5" />
<TextBlock Text="{Binding Path=Name}" Margin="5" />
<TextBlock Text="{Binding Path=Age}" Margin="5" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Grid.Column="0" Content="Name" Tag="Name" Click="SortButton_Click" />
<Button Grid.Row="1" Grid.Column="1" Content="Age" Tag="Age" Click="SortButton_Click" />
</Grid>
</Window>
後面的代碼:
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
namespace NumberedListBox
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Persons = new ObservableCollection<Person>();
Persons.Add(new Person() { Name = "Sally", Age = 34 });
Persons.Add(new Person() { Name = "Bob", Age = 18 });
Persons.Add(new Person() { Name = "Joe", Age = 72 });
Persons.Add(new Person() { Name = "Mary", Age = 12 });
CollectionViewSource view = FindResource("sortedPersonList") as CollectionViewSource;
view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
DataContext = this;
}
public ObservableCollection<Person> Persons { get; private set; }
private void SortButton_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
string sortProperty = button.Tag as string;
CollectionViewSource view = FindResource("sortedPersonList") as CollectionViewSource;
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription(sortProperty, ListSortDirection.Ascending));
view.View.Refresh();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
值轉換器:
using System;
using System.Windows.Data;
namespace NumberedListBox
{
public class RowNumberConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CollectionViewSource collectionViewSource = parameter as CollectionViewSource;
int counter = 1;
foreach (object item in collectionViewSource.View)
{
if (item == value)
{
return counter.ToString();
}
counter++;
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
呃,是不是會導致N^2行爲?對於屏幕上的每個項目,遍歷所有可能的項目以查找當前項目索引的索引。 – Armentage 2011-05-29 22:15:18