wpf - 如何在 wpf 中使 listview 自行更新?

标签 wpf data-binding listview

我意识到这是一个很长的问题,但我在这里除了发布我的代码之外别无他法,为了清楚起见,我尽量保持简短。当然,这样做违反了大量最佳实践,这个例子已经足够长了..

我制作了一个非常简单的 wpf 应用程序

  • 在屏幕左侧显示人员列表(格式:姓名和年龄介于 () 之间)
  • 在屏幕右侧显示所选人员的所有属性
  • 在右侧您可以编辑属性并在消息框中查看整个选择

在下面的示例中,我编辑了 Bar 的年龄。但是,在列表中,年龄没有更新。如果我询问基础集合,它似乎仍然已更新.. 我怎样才能让名单知道?

除了屏幕截图外,以下是代码和 XAML

注意:如果图片没有显示,请尝试在新标签页或窗口中打开它。


alt text


namespace ASAPBinding
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public override string ToString()
        {
            return String.Format("{0} ({1})",Name,Age);     
        }
    }

}

namespace ASAPBinding
{
    public class Dal
    {
        public ObservableCollection<Person> Persons { get; set; }

        public Dal()
        {
            Persons = new ObservableCollection<Person>();
            Persons.Add(new Person() {Name = "Bar", Age = 25});
            Persons.Add(new Person() {Name = "Foo", Age = 50});
        }

        public void PrintOutCollection()
        {
            MessageBox.Show(
                Persons[0].ToString() + "\n" + Persons[1].ToString()
                );
        }
    }
}

<Window x:Class="ASAPBinding.EditPersons"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ASAPBinding"
     x:Name="window1"
    Title="EditPersons" Height="300" Width="300">
    <Window.Resources>
        <local:Dal x:Key="dal"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>

        <ListBox Name="ListBox1" 
                 ItemsSource="{Binding Source={StaticResource dal}, Path=Persons, Mode=TwoWay}" 
                 Grid.Column="0"/>
        <StackPanel 
            DataContext="{Binding ElementName=ListBox1, Path=SelectedItem, Mode=TwoWay}"
            Grid.Column="1" Margin="0,0,0,108">

            <TextBox Text="{Binding Path=Name}" />
            <TextBox Text="{Binding Path=Age}"  />
            <Button Click="Button_Click">Show Collection</Button>
        </StackPanel>
    </Grid>
</Window>

public partial class EditPersons : Window
    {
        public EditPersons()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Dal dal = (Dal) window1.FindResource("dal");
            dal.PrintOutCollection();
        }
    }

最佳答案

仅仅有一个 ObservableCollection 是不够的,如果你想更新特定属性的绑定(bind),你的 Person 类型必须实现 INotifyPropertyChanged .

编辑

我刚刚注意到,您的左侧 ListBox 没有更新,因为您没有为 Person 对象设置 DataTemplate。您现在拥有的是一个 ToString() 实现,一旦它向 UI 报告,它就不会更新。

你需要这样的东西:

<DataTemplate DataType="{x:Type local:Person}">
   <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}"/>
        <TextBlock Text="("/>
        <TextBlock Text="{Binding Age}"/>
        <TextBlock Text=")"/>
    </StackPanel>
</DataTemplate>

关于wpf - 如何在 wpf 中使 listview 自行更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1131937/

相关文章:

.net - 如何在 WPF 类库上使用 .NET Framework 4.8?

c# - 使用 TabControl 和 MVVM 在 WPF 中正确进行数据绑定(bind)

android - 如何隐藏android listview中每个项目底部的水平线?

java - 从使用 ListView 填充的字符串数组中读取值

wpf - VirtualizingStackPanel 中 VirtualizationMode 属性的回收/标准之间的实际区别是什么?

wpf - 从字节数组创建 BitmapImage

WPF 绑定(bind) : Waiting for ItemsSource

javascript - Knockout ObserableArray 下拉绑定(bind)问题

listview - Win32 虚拟 ListView : Cannot select full row

wpf - Rx 响应式(Reactive)扩展 Observeondispatcher 单元测试错误 : The current thread has no Dispatcher associated with it