当ListView发生变化时,将TextBlock绑定到属性

本文关键字:TextBlock 绑定 属性 ListView 变化 | 更新日期: 2023-09-27 17:51:05

我就是想不明白。我还缺少什么来约束Textblock ?每次我在ListView中选择一个新项目时,我需要TextBlock更新。

这是我做的样品。在我的实际应用程序中,我使用ListView1的id从我的DB中获取我想在我的textBlock中显示的东西。

我知道WPF绑定到属性,我需要实现INotifyPropertyChanged,但我不能得到正确的绑定,或者也许我错过了别的什么?

我添加了DateTime.Now.TosString()只是为了更清楚地看到TextBlock是否变化。

XAML:

<Window x:Class="WpfSO.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" x:Name="txtBlockPerson" 
                                Text="{Binding MyPerson}" />

        <ListView Grid.Row="1" Grid.Column="0" x:Name="ListView1" 
                  ItemsSource="{Binding ListData}" 
                  IsSynchronizedWithCurrentItem="True" 
                  SelectionChanged="ListView1_SelectionChanged">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>
c#

using System;
using System.ComponentModel;
using System.Windows;
using System.Collections.ObjectModel;
namespace WpfSO
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<Person> ListData { get; set; }
        private const string _myName = "You clicked on: ";
        public Person MyPerson { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            // TextBlock
            MyPerson = new Person(_myName);
            txtBlockPerson.DataContext = MyPerson;
            // ListView
            ListData = new ObservableCollection<Person>();
            var p1 = new Person("p1");
            var p2 = new Person("p2");
            ListData.Add(p1);
            ListData.Add(p2);
            ListView1.ItemsSource = ListData;
        }
        private void ListView1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            MyPerson.Name = _myName + ListView1.SelectedItem + ". Time: " +DateTime.Now.ToString(); 
        }
    }
    public class Person : INotifyPropertyChanged
    {
        private string _name;
        public event PropertyChangedEventHandler PropertyChanged;
        public string Name
        {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    OnPropertyChanged("PersonName");
                }
            }
        }
        public Person(string name)
        {
            Name = name;
        }
        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

当ListView发生变化时,将TextBlock绑定到属性

OnPropertyChanged需要有正确的属性名称。

代替OnPropertyChanged("PersonName");
OnPropertyChanged("Name");