views:

5210

answers:

6

I need to define new UI Elements as well as data binding in code because they will be implemented after run-time. Here is a simplified version of what I am trying to do.

Data Model:

public class AddressBook : INotifyPropertyChanged
{
    private int _houseNumber;
    public int HouseNumber
    {
        get { return _houseNumber; }
        set { _houseNumber = value; NotifyPropertyChanged("HouseNumber"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string sProp)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
    }
}

Binding in Code:

AddressBook book = new AddressBook();
book.HouseNumber = 123;
TextBlock tb = new TextBlock();
Binding bind = new Binding("HouseNumber");
bind.Source = book;
bind.Mode = BindingMode.OneWay;
tb.SetBinding(TextBlock.TextProperty, bind); // Text block displays "123"
myGrid.Children.Add(tb);
book.HouseNumber = 456; // Text block displays "123" but PropertyChanged event fires

When the data is first bound, the text block is updated with the correct house number. Then, if I change the house number in code later, the book's PropertyChanged event fires, but the text block is not updated. Can anyone tell me why?

Thanks, Ben

+2  A: 

Do you need to set the binding mode programatically? It may be defaulting to OneTime.

Steven Robbins
Thanks for the suggestion, but adding:bind.Mode = BindingMode.OneWay;Has no effect.
Ben McIntosh
I may be misreading, but sounds like Beepcake means try setting the BindingMode to BindingMode.TwoWay. TextBlocks are OneWay by default.
Jab
+1  A: 

Are you sure you're updating the same object as you've used in the binding? At first glance nothing looks wrong, so check the simple things. :)

Giraffe
That was going to be my next suggestion :)
Steven Robbins
I created a completely unique object instance called "testing" and updated it once before binding and once after binding and the text block stayed at the value before binding even though the PropertyChanged event for "testing" fired.
Ben McIntosh
A: 

Does any code bypass the property, setting the field (_houseNumber) directly?

Marc Gravell
PropertyChanged woudln't fire then would it?
Steven Robbins
True, true... just trying to think why the text-block isn't updating...
Marc Gravell
Just make sure I wasn't being dim :) My money is on the code updating a different instance of AddressBook :)
Steven Robbins
+2  A: 

Make sure you're updating the AddressBook reference that was used in the binding, and not some other AddressBook reference.

I got the following to work with the AddressBook code you gave.

<StackPanel>
    <Button Click="Button_Click">Random</Button>
    <Grid x:Name="myGrid">
    </Grid>
</StackPanel>

Code behind:

public partial class Window1 : Window
{
    private AddressBook book;

    public Window1()
    {
        InitializeComponent();

        book = new AddressBook();
        book.HouseNumber = 13;

        TextBlock tb = new TextBlock();
        Binding bind = new Binding("HouseNumber");
        bind.Source = book;
        tb.SetBinding(TextBlock.TextProperty, bind);
        myGrid.Children.Add(tb);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Random rnd = new Random();
        book.HouseNumber = rnd.Next();
    }
}

Note the same reference is used in the update code.

Cameron MacFarland
+1  A: 

I've just cut+pasted your code (and added a little), and it works fine for me:

    public Window1()
    {
        InitializeComponent();

        AddressBook book = new AddressBook();
        book.HouseNumber = 123;
        TextBlock tb = new TextBlock();
        Binding bind = new Binding("HouseNumber");
        bind.Source = book;
        bind.Mode = BindingMode.OneWay;
        tb.SetBinding(TextBlock.TextProperty, bind); // Text block displays "123"
        myGrid.Children.Add(tb);
        book.HouseNumber = 456; 
    }

    private void TestButton_Click(object sender, RoutedEventArgs e)
    {
        AddressBook book = (AddressBook)((TextBlock)myGrid.Children[0]).GetBindingExpression(TextBlock.TextProperty).DataItem;

        book.HouseNumber++;
    }

Displays 456 on startup, clicking the button makes the number in the TextBlock increment just fine.

Perhaps you are looking in the wrong place for the cause of the problem?

Steven Robbins
+5  A: 

The root of it was that the string I passed to PropertyChangedEventArgs did not EXACTLY match the name of the property. I had something like this:

public int HouseNumber
{
    get { return _houseNumber; }
    set { _houseNumber = value; NotifyPropertyChanged("HouseNum"); }
}

Where it should be this:

public int HouseNumber
{
    get { return _houseNumber; }
    set { _houseNumber = value; NotifyPropertyChanged("HouseNumber"); }
}

Yikes! Thanks for the push in the right direction.

Ben McIntosh
Aha.. I've done the same myself when I've renamed a property :)The code you pasted in was correct though?
Steven Robbins
I changed the actual property names I was using the protect the innocent :)
Ben McIntosh
I was just about to suggest this as a common problem. INotifyPropertyChanged is so fragile. I wish we weren't forced to used such crud.
Daniel Paull