Re: simple binding of controls to object properties - refresh cont
From: Rua Haszard Morris (RuaHaszardMorris_at_discussions.microsoft.com)
Date: 10/19/04
- Next message: Ken: "CheckedListBox issue"
- Previous message: David Schmidt: "Re: opacity property not working"
- In reply to: Chris Jobson: "Re: simple binding of controls to object properties - refresh cont"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 19 Oct 2004 16:45:09 -0700
Thanks again Chris for looking into this.! Thanks Sijin for already
suggesting this (the correct?) approach.
This is now officially doing my head in - this doesn't work for me. I've
udpated my struct (class!) so that it has event handlers, suitably named (is
it documented anywhere that there is magic binding if eventhandlers are named
appropriately?), and the controls DON'T update!
Can anybody think of a reason why it isn't working for me? I have named
events correctly, but they are not getting set to anything...
Still I think I prefer just manually reseting the bindings, it seems like
bloat to add the eventhandlers & if (event) event() to each set(), anyway!
If the form class needs to handle all those events, forget it, I'll just
reset the bindings!
AND apparently it's all changing for the better in 2005...
My new (wishlist) question is can we define a parent class, which can be
inherited from to provide automatic binding-notification of property changes.
i.e.
class PropertyBindableBase
{
// the magic happens here
}
class SomeProperties : PropertyBindableBase
{
// a nice small simple class with properties, sets and gets
public string Ting
{
get { return stringy; };
set { stringy = value; };
}
private string stringy = "";
}
class BoundForm : Form
{
textbox BobTheBound = new TextBox();
SomeProperties pro = new SomeProperties();
...
Form()
{
BobTheBound.Bindings.Add("Text", someProperties, "Ting")
}
}
???
Anyway there's a nice long posting summing up my thoughts...
cheers all who helped,
Rua HM.
"Chris Jobson" wrote:
> I think I've now found the right way to do it. The web page
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet02252003.asp
> describes how to do it in VB, and I've just translated it to C# and it
> works. Basically if you have a property called Abc then you have to define
> an event named AbcChanged, and raise this event when your property changes
> (though this seems to be a very well-kept secret!!). Here's the trivial
> example that I tested it on:
>
> public class MyString {
> string s;
>
> public event EventHandler theStringChanged;
>
> public MyString(string s) {
> this.s = s;
> }
>
> public string theString {
> get { return s; }
> set {
> if (s != value) {
> s = value;
> if (theStringChanged != null)
> theStringChanged(this, new EventArgs());
> }
> }
> }
> }
> ....
> MyString myStr = new MyString("Hello world");
> ....
> textBox1.DataBindings.Add(new Binding("Text", myStr, "theString"));
>
> The textbox is now properly bound to the MyString object: assigning a new
> value to myStr.theString updates the textbox, and typing a new value in the
> textbox updates myStr.theString when the textBox loses focus.
>
> Chris Jobson
>
> "Chris Jobson" <chris.jobson@btinternet.com> wrote in message
> news:eNVkRSVtEHA.1276@TK2MSFTNGP12.phx.gbl...
> > As an experiment I managed to get this to work by implementing the
> > IBindingList interface in my class and raising the ListChanged event in
> > the property set code. However, this feels like the wrong solution because
> > to implement IBindingList you also have to implement IList, ICollection
> > and IEnumerable - and most of the methods in these interfaces are
> > irrelevant as the class is NOT actually implementing a list. If you want
> > to see the code email me and I'll send it to you off-list (but note that
> > I'll be off-line from the end of this week for about three weeks).
> >
> > Chris Jobson
> >
> > "Rua Haszard Morris" <RuaHaszardMorris@discussions.microsoft.com> wrote in
> > message news:E4E0044C-0CF8-46EA-8739-5BCE490F64F4@microsoft.com...
> >> Thanks everyone for all the help - ResumeBinding doesn't appear to do it
> >> after all, but clearing and reset ing all the bindings does, which is a
> >> not-so-bad workaround in my opinion.
> >>
> >> I figure that I'm probably using this (data binding) in a way it's not
> >> really supposed to be used... my "data source" is basically a struct with
> >> properties wrapping its members (which explains why I didn't want to
> >> fatten
> >> the class up with events!).
> >>
> >> Cheers,
> >> Rua HM.
> >
> >
>
>
>
- Next message: Ken: "CheckedListBox issue"
- Previous message: David Schmidt: "Re: opacity property not working"
- In reply to: Chris Jobson: "Re: simple binding of controls to object properties - refresh cont"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|