2012-04-13 5 views
0

목록 상자 항목 텍스트를 텍스트 상자에서 업데이트 한 후 업데이트하는 방법이 있습니까? 가능하다면 바인딩으로 만하고 싶습니다. 목록 상자는 목록에서 읽는 중이지만 목록이 업데이트되지 않으므로 목록에 새 항목을 추가하지 않으면 변경되지 않습니다. 여기에 코드wpf 바인딩 목록, 목록 상자 및 컨트롤 twoway

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

}

답변

0

목록 상자가 ToString() 표현하여 데인 클래스의 인스턴스를 보여주고있다. listbox를 osoba 속성에 직접 바인딩해야합니다 (DisplayMemberPath). 그리고 osoba 재산의 가치는 당신이 imia와 nazwisko를 바꾸면 udated되지 않습니다. getter에서 다시 계산하고 PropertiesChanged ("osoba")를 올린 다음 nazwisko 또는 imia 속성을 변경해야합니다.

+0

모든 것이 완벽합니다! 감사! 나는 그것이 단순한 실수라는 것을 알았지 만 나는 그것을 발견하기 위해 자질구레하지 않았다. 다시 한 번 감사드립니다. – lisek