views:

116

answers:

1

I can't figure out what is wrong for this simple binding. when click on first or second radio button, no first or last name display.

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) Code-behind

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

Thanks a lot!

+2  A: 

You need to actually set the binding source in code, to do that you need a name for the StackPanel. You must add the name to the last stack panel. The one in Row 1

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

In the event code update the DataContext

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

EDIT Clarified which StackPanel to name

JaredPar
Sorry, It doesn't work.By the way, I also added binding Source <TextBox Name="fn" Text="{Binding Source={StaticResource personInfo}, Path=FirstName}"/>
Southsouth
@northTiger I verified this works on my computer. Which StackPanel did you name? It must be the second one
JaredPar
Oh, it works after I remove Source={StaticResource personInfo}, from the TextBlock<TextBox Name="ln" Text="{Binding Source={StaticResource personInfo}, Path=LastName}"/>My question still, why doesn't Source={StaticResource personInfo} works if I do not add m_panel.DataContext = p;Thanks!
Southsouth
@northTiger, I believe the reason is because you don't ever actually update the personInfo value in the actual code.
JaredPar