2009-03-07 12 views
1

この単純なバインディングで何が間違っているかわかりません。 最初のラジオボタンまたは2番目のラジオボタンをクリックすると、名または姓が表示されません。コードビハインド簡単なデータバインディングの質問

1)XAML

<Window x:Class="WpfApplication2.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication2" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <local:Person x:Key="personInfo"/> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="50" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <StackPanel Orientation="Horizontal"> 
     <RadioButton Name="r1" Click="onClick1">1st person</RadioButton> 
     <RadioButton Name="r2" Click="onClick2">2nd person</RadioButton> 
     </StackPanel> 
     <StackPanel Grid.Row="1" Orientation="Horizontal"> 
      <TextBlock Name="t1">First Name</TextBlock>     
      <TextBox Name="fn" Text="{Binding Path=FirstName}"/> 

      <TextBlock Name="t2">Last Name</TextBlock> 
      <TextBox Name="ln" Text="{Binding Path=LastName}"/> 
     </StackPanel> 
    </Grid> 
</Window> 

2)

public partial class Window1 : Window 
    { 
     Person p = new Person(); 
     Person[] list ; 
     public Window1() 
     { 
      InitializeComponent(); 

      list = new Person[2] { new Person { FirstName = "John", LastName = "Smith"}, 
            new Person { FirstName = "Steve", LastName = "King"} }; 
     } 

     public void onClick1(Object h , RoutedEventArgs arg) 
     { 
      p.FirstName = list[0].FirstName; 
      p.LastName = list[0].LastName; 
     } 

     public void onClick2(Object h, RoutedEventArgs arg) 
     { 
      p = list[1]; 

     } 
    } 

3)Person.cs

class Person : INotifyPropertyChanged 
    { 
     string firstName; 
     string lastName; 

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

     public string LastName 
     { 
      get { return lastName; } 
      set { lastName = value; 
        OnPropertyChanged("LastName"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

どうもありがとう!

+0

に名前を付けるための1つのあなたは、デバッグ出力の結合のエラーに関するすべてのメッセージを見てきました?起こっていることを助けるかもしれないどんな声明ですか? – flq

答えて

2

StackPanelの名前が必要な場合は、実際にバインディングソースをコードに設定する必要があります。最後のスタックパネルに名前を追加する必要があります。行1イベントコードのアップデートで

<StackPanel 
    ... 
    Name="m_panel" /> 

のDataContext

public void onClick1(Object h , RoutedEventArgs arg) 
{ 
    p.FirstName = list[0].FirstName; 
    p.LastName = list[0].LastName; 
    m_panel.DataContext = p; 
} 

EDIT明確化のStackPanelが

+0

申し訳ありませんが、動作しません。 ところで、私はバインディングを追加しました。 Southsouth

+0

@northTigerどのStackPanelに名前を付けましたか?それは2番目のものでなければならない – JaredPar

+0

ああ、それはTextBlockからSource = {StaticResource personInfo}を削除した後に動作する 私の質問はまだ、私はm_panel.DataContext = pを追加しないと、Source = {StaticResource personInfo}は動作しません。 ありがとう! – Southsouth