databinding properties to controls

J

Jason Wolf

I have added application settings for a few user options such as
ShowFramerate.
I have a toolstrip with menu checkable menu items and have bound their
checked property to the relevant applicationSetting.

The databinding works as far as initially loading the setting into the
control, but when the controll is changed, the property is not updated, so I
have to add something like...
onCheckedStateChanged:
Properties.Settings.Default.ShowFramerate=new value

....when I was really hoping that the setting was reading the bound property
and not just writing it. That is... I would like that when the checked
property is changed, the setting, bound to the checked state, is updated.
Can this be done for me, or must I add the code myself?
 
M

Marc Gravell

App-settings bindings are one-way; if you look at the designer.cs, it
simply reads, i.e.

this.testMenuItem.Checked =
global::WindowsFormsApplication1.Properties.Settings.Default.TestSetting;

You would need to wire it differently if you wanted it to write back.
There is no DataBindings property for ToolStripMenuItem. Perhaps
simply hook the CheckedChanged event of all the menu-items to a shared
handler that identifies the menu from the sender and updates
accordingly?

Marc
 
J

Jason Wolf

Perhaps
simply hook the CheckedChanged event of all the menu-items to a shared
handler that identifies the menu from the sender and updates
accordingly?

Yas that would probably be an accetable solution to have one handler with a
switch of the relevant senders. I was just hoping tht with databinding it
would be truely bound so an active change one place would reflect the other.
Thanks for the feedback
 
M

Marc Gravell

I was just hoping tht with databinding it
would be truely bound

If the menu-item supported data-binding, it would be; for example
binding to a regular CheckBox or TextBox would work [it would generate
some DataBindings.Add(...) code].

You can hook it together manually (using PropertyChanged on the
settings, and CheckedChanged on the menu-item), but if you do you need
to be careful to unsubscribe when the form is disposed.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top