Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编号列表框_C#_Wpf_Listbox - Fatal编程技术网

C# 编号列表框

C# 编号列表框,c#,wpf,listbox,C#,Wpf,Listbox,我有一个排序的列表框,需要显示每个项目的行号。在这个演示中,我有一个带有名称字符串属性的Person类。列表框显示按姓名排序的人员列表。如何将行号添加到列表框的datatemplate XAML: 代码隐藏: using System; using System.Collections.ObjectModel; using System.Windows.Data; using System.Windows; using System.ComponentModel; namespace Nu

我有一个排序的列表框,需要显示每个项目的行号。在这个演示中,我有一个带有名称字符串属性的Person类。列表框显示按姓名排序的人员列表。如何将行号添加到列表框的datatemplate

XAML:


代码隐藏:

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; }
    }
}
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; }
    }
}
使用系统;
使用System.Collections.ObjectModel;
使用System.Windows.Data;
使用System.Windows;
使用系统组件模型;
命名空间NumberedListBox
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
人员=新的可观察集合();
添加(newperson(){Name=“Sally”});
添加(newperson(){Name=“Bob”});
添加(newperson(){Name=“Joe”});
添加(newperson(){Name=“Mary”});
PersonListCollectionView=新建ListCollectionView(个人);
PersonListCollectionView.SortDescriptions.Add(新的SortDescription(“Name”,ListSortDirection.升序));
DataContext=this;
}
公共可观察集合人{get;private set;}
public ListCollectionView PersonListCollectionView{get;private set;}
}
公共阶层人士
{
公共字符串名称{get;set;}
}
}

这应该让您开始:


它说它是为Silverlight设计的,但我不明白为什么它对WPF不起作用。基本上,您可以将文本块绑定到数据,并使用自定义值转换器输出当前项目的编号。

这应该可以让您开始:


它说它是为Silverlight设计的,但我不明白为什么它对WPF不起作用。基本上,您可以将文本块绑定到数据,并使用自定义值转换器输出当前项目的编号。

David Brown链接中的想法是使用一个有效的值转换器。下面是一个完整的工作示例。列表框有行号,可以根据姓名和年龄进行排序

XAML:


代码隐藏:

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; }
    }
}
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; }
    }
}

David Brown链接中的想法是使用一个有效的值转换器。下面是一个完整的工作示例。列表框有行号,可以根据姓名和年龄进行排序

XAML:


代码隐藏:

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; }
    }
}
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; }
    }
}

终于!如果找到一种更优雅的方式,可能也有更好的性能。 (另见)

为此,我们“误用”了属性
ItemsControl.AlternateIndex
。最初,它旨在以不同方式处理
列表框中的每一行。(见附件)

1。将AlternatingCount设置为列表框中包含的项目数量


2。绑定到数据模板的交替索引


...

因此,无需转换器,无需额外的
CollectionViewSource
,最重要的是无需强制搜索源集合。

最后!如果找到一种更优雅的方式,可能也有更好的性能。 (另见)

为此,我们“误用”了属性
ItemsControl.AlternateIndex
。最初,它旨在以不同方式处理
列表框中的每一行。(见附件)

1。将AlternatingCount设置为列表框中包含的项目数量


2。绑定到数据模板的交替索引


...

因此,无需转换器、额外的
CollectionViewSource
,最重要的是无需对源代码集合进行暴力搜索。

这是另一个答案。我尝试了上面的方法,它在WPF(
AlternationCount
solution)中工作,但是我需要Silverlight的代码,所以我做了以下工作。这比其他蛮力方法更优雅


后面

  using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.Linq;
  using System.Windows.Controls;
  using System.Windows.Controls.Primitives;
  using System.Windows.Data;

  namespace RowNumber
  {
     public class ListItemIndexConverter : IValueConverter
     {
        // Value should be ListBoxItem that contains the current record. RelativeSource={RelativeSource AncestorType=ListBoxItem}
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
           var lbi = (ListBoxItem)value;
           var listBox = lbi.GetVisualAncestors().OfType<ListBox>().First();
           var index = listBox.ItemContainerGenerator.IndexFromContainer(lbi);
           // One based. Remove +1 for Zero based array.
           return index + 1;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
     }
     public partial class MainPage : UserControl
     {
        public MainPage()
        {
           // Required to initialize variables
           InitializeComponent();
        }
        public List<string> Test { get { return new[] { "Foo", "Bar", "Baz" }.ToList(); } }
     }
  }
使用系统;
使用System.Collections.Generic;
利用制度全球化;
使用System.Linq;
使用System.Windows.Controls;
使用System.Windows.Controls.Primitives;
使用System.Windows.Data;
命名空间行数
{
公共类ListItemIndexConverter:IValueConverter
{
//值应为包含当前记录的ListBoxItem。RelativeSource={RelativeSource AncestorType=ListBoxItem}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var lbi=(ListBoxItem)值;
var listBox=lbi.getVisualancetors().OfType().First();
var index=listBox.ItemContainerGenerator.IndexFromContainer(lbi);
//基于一的。对于基于零的数组,删除+1。
收益率指数+1;
}
公共对象ConvertBack(对象值、类型targetType、对象参数、CultureInfo区域性){抛出新的NotImplementedException();}
}
公共部分类主页面:UserControl
{
公共主页()
{
//需要初始化变量
初始化组件();
}
公共列表测试{get{返回新[]{“Foo”、“Bar”、“Baz”