2009/04/16(Thu) 00:09:29 編集(投稿者)
今日の成果ですが、こんなソースしかかけませんでした><
しかも、ソートしてくれなーいヽ( ̄∇ ̄;)ノ
ちょっと休憩。
namespace sortlistbox
{
/// <summary>
/// Window1.xaml の相互作用ロジック
/// </summary>
public partial class Window1 : Window
{
ObservableCollection<Person> list = new ObservableCollection<Person>();
CollectionViewSource list2 = new CollectionViewSource();
public Window1()
{
InitializeComponent();
Person p1 = new Person();
p1.Name = "name1";
Person p2 = new Person();
p2.Name = "name3";
Person p3 = new Person();
p3.Name = "name5";
Person p4 = new Person();
p4.Name = "name4";
Person p5 = new Person();
p5.Name = "name2";
list.Add(p1);
list.Add(p2);
list.Add(p3);
list.Add(p4);
list.Add(p5);
list2.Source = list;
listBox1.DataContext = list;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
list2.SortDescriptions.Clear();
list2.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Descending));
}
}
public class Person
{
public string Name { set; get; }
}
}
<Window x:Class="sortlistbox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:sortlistbox"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="personViewTemplate" DataType="{x:Type local:Person}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="さん " />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding}" Margin="76,68,82,94" Name="listBox1" ItemTemplate="{StaticResource personViewTemplate}"/>
<Button Height="23" Margin="104,19,99,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">Button</Button>
</Grid>
</Window>
listBox1.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Descending));
↑こっちだとソートしてくれました。CollectionViewSourceの使い方がわからない^^;