views:

45

answers:

2

Is there anyone who knows this?

I have been trying this for the last week, and no luck.

Now I can see that once can bind successfully to a Button's Text property, but not its ImageKey property:

myButton.Text = "new text"; // really changes the bound data
myButton.ImageKey = "new text"; // does NOT change the bound data

I use:

myButton.DataBindings.Add ( new Binding ( "ImageKey", this.MyData, "Name", true, DataSourceUpdateMode.OnPropertyChanged ) );

Why? What makes the Binding tick/work? I just don't get it.

EDIT:

OK so I defined these for my own derived control:

public event EventHandler ImageKeyChanged;

protected virtual void OnImageKeyChanged ( EventArgs e )
{
    if ( ImageKeyChanged!= null )
    {
     ImageKeyChanged ( this, e );
    }
}

[Bindable ( true )]
public new string ImageKey
{
    get
    {
     return base.ImageKey;
    }
    set
    {
     base.ImageKey = value;
     this.OnImageKeyChanged ( EventArgs.Empty );
    }
}

It still doesn't work. Is there a tutorial or something on the net, that shows this. It just doesn't work for me.

+2  A: 

...does NOT change the bound data

for 2-way binding, you need notification events - this can take 2 common forms:

  • a public event EventHandler {name}Changed;, where {name} is the bound property (ImageKeyChanged, for example)
  • the object can implement INotifyPropertyChanged

This is certainly how object-to-control binding works; I'm pretty sure that control-to-object detection is very similar. Note that there is a TextChanged event, but no ImageKeyChanged event.

Marc Gravell
Thanks Marc. I implemented the event, and also had the INotifyPropertyChanged for the object that's bound before. But still the same. I must be missing something.
Joan Venge
Actually I think it worked. I always have this at the end of the day, will check for another property.
Joan Venge
Ok it seems like another property I added other than ImageKey doesn't work. Do you know why this might be? So I bind ImageKey to Name, and it worked after I did your changes. But now I wrote exactly the same code for another property (without new, as this property doesn't exist in the base), but now the same bound data property doesn't change.
Joan Venge
Nevermind I think I got it working with these suggestions. Sorry it's my bad, but it's you and Lucero's replies that helped me out.
Joan Venge
+1  A: 

For bindings to work, the class with the property to be bound needs to supply change events (TextChanged for the Text property for instance) or implement INotifyPropertyChanged. Since the Button class doesn't supply a ImageKeyChanged event, the binding cannot subscribe to get notified of the change.

Lucero
Thanks Lucero, I think I got it working. Thanks to you and Marc. I want to accept both answers.
Joan Venge