【问题标题】:WPF: bind to the first item in the sorted ListViewWPF:绑定到排序的 ListView 中的第一项
【发布时间】:2014-06-25 20:10:27
【问题描述】:

我有一个包含项目的 ListView,其中包含字符串字段 Name 等。 ListView 中的项目按此字段排序:

SortDescription descr = new SortDescription("Name", ListSortDirection.Ascending);
list.Items.SortDescriptions.Add(descr);

我还有一个 TextBlock,我想在其中显示已排序 ListView 中第一项的名称。可以在运行时添加、删除和编辑项目,所以我想使用某种绑定,如下所示(不起作用,仅作为示例):

<TextBlock Text="{Binding ElementName=list, Path=Items[0].Name}"/>

1) 如何使用绑定实现所需的行为?

2) 如果无法创建这样的绑定,那么成功的最方便的方法是什么?

任何想法和提示将不胜感激。

更新

主窗口内容:

<StackPanel>
    <TextBlock Name="nameFirst" Text="{Binding ElementName=list, Path=Items[0].Name}"/>
    <ListView Name="list" ItemsSource="{Binding ElementName=mainWnd, Path=List}" DisplayMemberPath="Name" Loaded="list_Loaded"/>
</StackPanel>

后面的代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public class Item : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public Item(string name)
        {
            Name = name;
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set 
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    public ObservableCollection<Item> _list;
    public ObservableCollection<Item> List
    {
        get { return _list; }
        set 
        { 
            _list = value;
            OnPropertyChanged("List");
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        List = new ObservableCollection<Item>();
        List.Add(new Item("1"));
        List.Add(new Item("2"));
        List.Add(new Item("3"));
    }

    private void list_Loaded(object sender, RoutedEventArgs e)
    {
        SortDescription descr = new SortDescription("Name", ListSortDirection.Descending);
        list.Items.SortDescriptions.Add(descr);
    }
}

应用程序启动时,ListView 中的项目按降序排列:“3”、“2”、“1”,但 nameFirst TextBox 仍显示“1”,但现在应该显示“3”。

【问题讨论】:

    标签: wpf listview sorting binding


    【解决方案1】:

    实际上,您所写的内容应该可以完成工作。问题可能不在 xaml 中。

    您说项目包含一个字段名称。 WPF 只能绑定到公共属性,请确保使用它们,而不是公共成员。如果您在打开窗口后更改了旧值,您也可能会看到旧值。确保您的项目实现INotifyPropertyChanged 接口,并且属性调用OnPropertyChanged

    如果您仍然遇到问题,请发布更多代码,特别是您的数据项类和设置 SortDescriptor 的位置。

    最后,虽然这不是你问的。您可以使用/ 运算符绑定到数组中的current 项。要使用您的代码:

    <TextBlock Text="{Binding ElementName=list, Path=Items/Name}"/>
    

    您可以使用CollectionViewsIsSynchronizedWithCurrentItem 属性控制“当前”项。

    【讨论】:

    • 是的,绑定Binding ElementName=list, Path=Items[0].Name 将文本绑定到Name 属性,但如果项目已排序,则不会更新。例如,有两个项目:“2”和“1”(按照它们在 ListView 源中的排列顺序)。对项目进行排序时,“1”位于“2”之前,但 Item[0] 仍为“2”。
    • 抱歉,更准确地说,Item[0] 将在内部更改为“1”,但绑定不会更新,仍会将文本绑定到“2”值。跨度>
    • @undermind - 尝试使用 ObservableCollection 作为列表 ItemsSource 的集合。请发布更多您的代码。问题不在您发布的代码中,我只能猜测其余部分。
    • 用代码更新了问题。抱歉耽搁了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2011-01-14
    • 2019-04-28
    • 2017-04-07
    相关资源
    最近更新 更多