0

在文本框中更新列表框项目文本后,有什么方法可以更新它?如果可能的话,我只想用绑定来做。列表框正在从列表中读取,但列表没有更新,所以它永远不会改变,除非我将新项目添加到列表中。这是代码

<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);
    }
}

}

4

1 回答 1

0

列表框通过 ToString() 表示形式显示您的 dane 类的实例。您应该osoba直接使用 将列表框绑定到属性DisplayMemberPath。而且 osoba 财产的价值也没有更新,然后你改变了 imia 和 nazwisko。您应该在它的 getter 上重新计算它并引发 PropertChanged("osoba") 然后属性 nazwisko 或 imia 正在被更改。

于 2012-04-13T20:51:28.800 回答