WPF-prosty przykład- problem z wiązaniem danych

0

Witam,

Przerabiam podręcznik do WPFa i mam problem ze zrealizowaniem takiego zadania (przykład jest opisany trochę po łebkach):
Mamy klasę Person z polami: FirstName i LasName. Gdy naciskamy na przycisk wartości pól w klasie Person zmieniają swoje wartości. Chcę, aby za pomocą wiązania danych zmiany wartości pól były odzwierciedlane (widoczne) w Labelu.

Klasa Person:

public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string firstName;
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value;
                    OnPropertyChanged(firstName);
                }
        }
        private string lastName;
        public string LastName
        {
            get { return lastName; }
            set
            {
                lastName = value;
                OnPropertyChanged(lastName);
                
            }
        }
        
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

        }
    } 

Klasa Okna oraz kod XAML:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFExamples
{
    /// <summary>
    /// Interaction logic for BindingExample.xaml
    /// </summary>
    public partial class BindingExample : Window
    {
        public Person Person;

        public BindingExample()
        {
            InitializeComponent();
            Person = new Person();
            Person.PropertyChanged += Metoda;
            Person.FirstName = "Jan";
            Person.LastName = "Kowalski";
            DataContext = Person;
        }

        public void Metoda(object o, PropertyChangedEventArgs e)
        {
            MessageBox.Show("Zmieniono wartość na: " + e.PropertyName);
        }

        private void ChangeProperty(object sender, RoutedEventArgs e)
        {
            Person.FirstName = "Pawel";
            Person.LastName = "Nowak";
          
        }
    }
} 
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFExamples" x:Class="WPFExamples.BindingExample"
        Title="BindingExample" Height="300" Width="300">
    <Window.DataContext>
        <local:Person/>
    </Window.DataContext>
    <Canvas>
        <TextBox x:Name="PoleTekstowe"  Canvas.Top="100"  Width="292" MaxLength="140"/>
        <Label Content="{Binding Text.Length, ElementName=PoleTekstowe}"  Canvas.Top="150" Width="292" Height="28"/>
        <Label Content="{Binding Path=FirstName}" Background="yellow"  Canvas.Top="200" Width="292" Height="28" />
        <Button Content="ChangeProperty" Click="ChangeProperty"/>
    </Canvas>
</Window> 

Wartości pól w klasie Person zmieniają się poprawnie. Jednak Content Labela wyświetla ciągle wartość "Jan". Co robię źle?

0

Imo w OnPropertyChanged powinieneś mieć nazwę zmiennej a nie jej wartość. Zmień na OnPropertyChanged("LastName") i sprawdź.

1 użytkowników online, w tym zalogowanych: 0, gości: 1