0

Is there any way to update listbox items text after updating it in textboxes? I wanna do it only with bindings if it is possible. Listbox is reading from list, but list isn't updating so it's never gonna change, unless I add new item to list. Here is code

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DockPanel VerticalAlignment="Top" HorizontalAlignment="Left">
        <Button Name="dodaj" Width="40" Margin="5" Click="dodaj_Click">Dodaj</Button>
        <Button Name="usun" Width="40" Margin=" 5" Click="usun_Click">Usuń</Button>
    </DockPanel>
    <DockPanel HorizontalAlignment="Left" VerticalAlignment="Center" DataContext="{Binding ElementName=lista, Path=osoba, Mode=TwoWay}">
        <ListBox Name="lb" ItemsSource="{Binding}" Width="200" Height="230">
        </ListBox>
    </DockPanel>
    <DockPanel HorizontalAlignment="Right" VerticalAlignment="top">
        <WrapPanel>
        <StackPanel>
        <Label>Imię</Label>
        <Label>Nazwisko</Label>
        <Label>Email</Label>
        <Label>Telefon</Label>
        </StackPanel>
            <StackPanel DataContext="{Binding ElementName=lb, Path=SelectedItem, UpdateSourceTrigger=LostFocus}" TextBoxBase.TextChanged="zmiana">
                <TextBox Width="200" Margin="3" Name="imie" Text="{Binding Path=imie}"></TextBox>
                <TextBox Width="200" Name="nazwisko" Text="{Binding Path=nazwisko}"></TextBox>
                <TextBox Width="200" Margin="3" Name="email" Text="{Binding Path=email}"></TextBox>
                <TextBox Width="200" Name="telefon" Text="{Binding Path=telefon}"></TextBox>
            </StackPanel>
        </WrapPanel>
    </DockPanel>
</Grid>

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;




namespace WpfApplication1
{


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        lista.Add(new dane("imie1", "nazwisko1", "email1", "tel1"));
        lista.Add(new dane("imie2", "nazwisko2", "email2", "tel2"));
        lb.DataContext = lista;
    }
    public class dane : INotifyPropertyChanged
    {
        public dane(string imie, string nazwisko, string email, string telefon)
        {
            this._imie = imie;
            this.nazwisko = nazwisko;
            this.osoba = nazwisko + " " + imie;
            this.email = email;
            this.telefon = telefon;

        }
        private string _imie;
        public string imie
        {
            set
            {
                _imie = value;
                OnPropertyChanged("Imie");
            }
            get
            {
                return _imie;
            }
        }
        public string nazwisko { set; get; }
        public string osoba { set; get; }
        public string email { set; get; }
        public string telefon { set; get; }
        public override string ToString()
        {
            return osoba;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string value)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(value));
            }
        }

    }
    public ObservableCollection<dane> lista = new ObservableCollection<dane>();

    //public List<dane> lista = new List<dane>();
    /*public ObservableCollection<dane> lista
    {
        get { return (ObservableCollection<dane>)GetValue(listaProperty); }
        set { SetValue(listaProperty, value); }
    }

    public static readonly DependencyProperty listaProperty =
        DependencyProperty.Register("lista", typeof(ObservableCollection<dane>), typeof(Window), new UIPropertyMetadata(null));
    */
    private void dodaj_Click(object sender, RoutedEventArgs e)
    {
        lista.Add(new dane("...", "", "", ""));
        MessageBox.Show(lista[0].ToString());

    }

    private void usun_Click(object sender, RoutedEventArgs e)
    {
        lista.RemoveAt(lb.SelectedIndex);
    }
}

}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
lisek
  • 267
  • 4
  • 16

1 Answers1

0

Listbox shows instances of your dane class by their ToString() representation. You should bind listbox to osoba property directly with DisplayMemberPath. And also value of osoba property is not udated then you change imia and nazwisko. You should recalculate it on its getter and also raise PropertChanged("osoba") then properties nazwisko or imia are being changed.

Nikolay
  • 3,658
  • 1
  • 24
  • 25
  • Everything worsk perfect! Thanks! I knew it was a simple mistake but I wasn't abble to find it. Thanks one more time. – lisek Apr 14 '12 at 11:41