HighTechTalks DotNet Forums  

Bind toolstrip buttons to props in a class

Dotnet Framework (WinForms DataBinding) microsoft.public.dotnet.framework.windowsforms.databinding


Discuss Bind toolstrip buttons to props in a class in the Dotnet Framework (WinForms DataBinding) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
moondaddy
 
Posts: n/a

Default Bind toolstrip buttons to props in a class - 04-24-2006 , 01:43 PM






I have a c# 2.0 app and want to bind the buttons on a toolstrip to
properties in a class which will manage the state of these buttons. How can
this be done?

Thanks.


--
moondaddy (AT) noemail (DOT) noemail



Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default RE: Bind toolstrip buttons to props in a class - 04-25-2006 , 04:15 AM






Hi,

Thank you for posting. From your post, my understanding on this issue is:
how to bind toolstrip buttons to the properties in a business object. If
I'm off base, please feel free to let me know.

To bind a component to a data source, the component must implement the
interface IBindableComponent. Unfortunately, the ToolStripItem class
itself doesn't implement IBindableComponent. So we can't bind a toolstrip
item to a property in a business object directly. The only way to make a
toolstrip item support databinding is creating a new class inherited from
the ToolStripItem class and the interface IBindableComponent.

However, there are several components which inherite from theToolStripItem
class, such as ToolStripButton, ToolStripTextBox, ToolStripLabel and so on.
If the new class is inherited from the ToolStripItem class, it hasn't the
specific features of ToolStripButton or ToolStripTextBox. I mean if you
want to make a toolstrip button support databinding, you'd better make the
new class inherite from ToolStripButton.

The following is a sample of a new class named ToolStripButton_Ext which
is inheirted from the ToolStripButton class and the interface
IBindableComponent .

class ToolStripButton_Ext:ToolStripButton,IBindableCompo nent
{
#region IBindableComponent Members

private BindingContext bindingContext;
private ControlBindingsCollection dataBindings;

public BindingContext BindingContext
{
get
{
if (bindingContext == null)
{
bindingContext = new BindingContext();
}
return bindingContext;

}
set
{
this.bindingContext = value;
}
}

public ControlBindingsCollection DataBindings
{
get
{
if (dataBindings == null)
{
dataBindings = new ControlBindingsCollection(this);
}
return dataBindings;
}
}

#endregion
}

In your winform, you may use the following sentences to bind a
ToolStripButton_Ext to a property in the business object.

// definition of the class to bind
public class Person
{
private string personID;
private string name;
public Person(string id, string name)
{
personID = id;
this.name = name;
}
public string PersonID
{
get { return this.personID; }
set { this.personID = value; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
private void Form1_Load(object sender, EventArgs e)
{
person = new Person("1", "microsoft msdn");
ToolStripButton_Ext toolstripitem = new ToolStripButton_Ext();

// bind the ToolTipText of the ToolStripButton to the
Person.Name
Binding binding = new
Binding("ToolTipText",this.person,"Name");
toolstripitem.DataBindings.Add(binding);
this.toolStrip1.Items.Add(toolstripitem);
}

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to tell me.


Sincerely,
Linda Liu
Microsoft Online Community Support

================================================== ==
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
================================================== ==


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.