编号列表框 [英] Numbered listbox
问题描述
我有一个排序列表框,需要显示每个项目的行数。在这个演示中,我有一个名称字符串属性Person类。目录表显示按名称排序的人士的名单。我怎么可以添加到列表框的DataTemplate中的行数???
XAML:
<窗口x:类=NumberedListBox.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
HEIGHT =300WIDTH =300>
<列表框
的ItemsSource ={绑定路径= PersonsListCollectionView}
HorizontalContentAlignment =拉伸>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={绑定路径=名称}/>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
< /窗GT;
背后code:
使用系统;
使用System.Collections.ObjectModel;
使用System.Windows.Data;
使用System.Windows;
使用System.ComponentModel;命名空间NumberedListBox
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent(); 人=新的ObservableCollection<&人GT;();
Persons.Add(新的Person(){名称=莎莉});
Persons.Add(新的Person(){名称=鲍勃});
Persons.Add(新的Person(){名称=乔});
Persons.Add(新的Person(){名称=玛丽}); PersonsListCollectionView =新的ListCollectionView(人);
PersonsListCollectionView.SortDescriptions.Add(新SortDescription(名,ListSortDirection.Ascending)); 的DataContext =这一点;
} 公众的ObservableCollection<&人GT;人{搞定;私人集; }
公众的ListCollectionView PersonsListCollectionView {搞定;私人集; }
} 公共类Person
{
公共字符串名称{;组; }
}
}
这应该让你开始:
<一个href=\"http://weblogs.asp.net/h$p$pishuber/archive/2008/11/18/rownumber-in-silverlight-datagrid-or-listbox.aspx\" rel=\"nofollow\">http://weblogs.asp.net/h$p$pishuber/archive/2008/11/18/rownumber-in-silverlight-datagrid-or-listbox.aspx
它说,它是Silverlight的,但我不明白为什么它不会为WPF工作。基本上,你一个TextBlock绑定到你的数据和使用自定义值转换器输出当前项目的数量。
I have a sorted listbox and need to display each item's row number. In this demo I have a Person class with a Name string property. The listbox displays a a list of Persons sorted by Name. How can I add to the datatemplate of the listbox the row number???
XAML:
<Window x:Class="NumberedListBox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<ListBox
ItemsSource="{Binding Path=PersonsListCollectionView}"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>
Code behind:
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows;
using System.ComponentModel;
namespace NumberedListBox
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Persons = new ObservableCollection<Person>();
Persons.Add(new Person() { Name = "Sally"});
Persons.Add(new Person() { Name = "Bob" });
Persons.Add(new Person() { Name = "Joe" });
Persons.Add(new Person() { Name = "Mary" });
PersonsListCollectionView = new ListCollectionView(Persons);
PersonsListCollectionView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
DataContext = this;
}
public ObservableCollection<Person> Persons { get; private set; }
public ListCollectionView PersonsListCollectionView { get; private set; }
}
public class Person
{
public string Name { get; set; }
}
}
This should get you started:
It says it's for Silverlight, but I don't see why it wouldn't work for WPF. Basically, you bind a TextBlock to your data and use a custom value converter to output the current item's number.
这篇关于编号列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!