2013-01-18 32 views
2

我按照一對夫婦的Microsoft網站上的例子,最早涉及的ItemsSource:雙向綁定,實現INotifyPropertyChanged的一類,沒有任何反應

ItemsControl.ItemsSource Property 第二:

How to: Implement Property Change Notification

togheter合併這兩個例子我已經到達我的例子:

XAML

<Window x:Class="WpfSchede.Test.ItemsSourceTest" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:c="clr-namespace:WpfSchede.Test" 
    Title="ItemsSourceTest" Height="352" Width="467"> 
<Window.Resources> 
    <c:NameList x:Key="NameListData"></c:NameList> 
    <DataTemplate x:Key="NameItemTemplate"> 
     <TextBlock Width="200" Text="{Binding Path=FullName, Mode=TwoWay}" /> 
    </DataTemplate> 
</Window.Resources>     
<Grid> 
    <ListBox ItemsSource="{Binding Source={StaticResource NameListData}}" ItemTemplate="{StaticResource NameItemTemplate}" Margin="12,12,120,65" Name="listBoxNames" IsSynchronizedWithCurrentItem="True"> 
    </ListBox> 
    <Button Content="Change First Name" Height="23" HorizontalAlignment="Left" Margin="12,270,0,0" Name="buttonChangeName" VerticalAlignment="Top" Width="163" Click="buttonChangeName_Click" /> 
    <Button Content="Change Full Name" Height="23" HorizontalAlignment="Left" Margin="193,270,0,0" Name="buttonChangeFullName" VerticalAlignment="Top" Width="163" Click="buttonChangeFullName_Click" /> 
</Grid> 

XAML.CS

using System.Windows; 
namespace WpfSchede.Test 
{ 
public partial class ItemsSourceTest : Window 
{ 
    public ItemsSourceTest() 
    { 
     InitializeComponent(); 
    } 

    private void buttonChangeName_Click(object sender, RoutedEventArgs e) 
    { 
     PersonName p = listBoxNames.SelectedItem as PersonName; 
     p.FirstName = "Andrew"; 
    } 

    private void buttonChangeFullName_Click(object sender, RoutedEventArgs e) 
    { 
     PersonName p = listBoxNames.SelectedItem as PersonName; 
     p.FullName = "Andrea Rossi"; 
    } 
    } 
} 

NameList中類

using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace WpfSchede.Test 
{ 
public class NameList : ObservableCollection<PersonName> 
{ 
    public NameList() 
     : base() 
    { 
     Add(new PersonName("Wil", "Cath")); 
     Add(new PersonName("Isak", "Dinesen")); 
     Add(new PersonName("Victor", "Hugo")); 
     Add(new PersonName("Jules", "Verne")); 
     Add(new PersonName("Leonardo", "Rossi")); 
    } 
} 

public class PersonName : INotifyPropertyChanged 
{ 
    private string firstName; 
    private string lastName; 
    // Define the event 
    public event PropertyChangedEventHandler PropertyChanged; 

    public PersonName(string first, string last) 
    { 
     this.firstName = first; 
     this.lastName = last; 
    } 

    public string FirstName 
    { 
     get { return firstName; } 
     set { 
      firstName = value; 
      OnPropertyChanged(value); 
     } 
    } 

    public string LastName 
    { 
     get { return lastName; } 
     set { lastName = value; } 
    } 

    public string FullName 
    { 
     set { 
      string full = value as string; 
      char[] sep = { ' ' }; 
      string[] n = full.Split(sep); 
      this.FirstName = n[0]; 
      this.LastName = n[1]; 
      OnPropertyChanged(value); 
     } 
     get { return firstName + " " + lastName; } 
    } 

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

    } 
} 
} 

我有兩個問題。

首先)當點擊其中一個按鈕時,我期待ListBox反映更改,而不會發生任何反應!我錯過了什麼?

二)在我綁定的全名(綁定到只有名字的DataTemplate中本來用於例如爲了更容易,但在我看來,像太簡化)。所以我選擇了綁定到Property FullName,爲此我在NameList類和DataTemplate代碼中編寫了自定義代碼。我的代碼有什麼問題?在DataTemplate中指定Mode = TwoWay是否正確?

回答

1

OnPropertyChanged(value);是錯誤的

它應該是OnPropertyChanged(「FirstName」); 也應該跟着 OnPropertyChanged(「FullName」);導致

OnPropertyChanged("FirstName"); 
OnPropertyChanged("FullName"); 

姓氏設定的功能應該有

OnPropertyChanged("LastName"); 
OnPropertyChanged("FullName"); 

最後設置的功能應該有

OnPropertyChanged("FullName"); 
OnPropertyChanged("FirstName"); 
OnPropertyChanged("LastName"); 
相關問題