View previous topic :: View next topic |
Author |
Message |
GreyCloud
Joined: 13 Nov 2008 Posts: 19
|
Posted: Mon Sep 13, 2010 4:14 pm Post subject: Column of Buttons |
|
|
Hey there,
do you have a sample / any pointers for how to have a column of buttons - effectively i want to add a delete button to each row. Bonus points if i can have more than one button in each column (eg move up /move down)
Many thanks
David |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Sep 13, 2010 10:52 pm Post subject: |
|
|
To do this you need to add an Editor (via the Virtual Tree designer) that uses a System.Windows.Forms.Button as the editor control. Set the DisplayMode for the Editor to "Always" then set the Editor property of the column you want it to display in.
The ValueProperty of the Editor will by default be set to the "Text" property of the button. However because you probably won't map any data to this column this would cause the Text of the button to be set to an empty string. To avoid this you can set the ValueProperty to something else that won't affect the button - I'd suggest the "Tag" property.
To handle the button clicks you need to attach event handlers to the button. Because the button you define in the editor is just a template attaching event handlers to it won't work. See the following post for more details on how to do this using InitializeControl:
http://www.infralution.com/phpBB2/viewtopic.php?t=1884
If you want two buttons in a column you could define your own control which has the two buttons in it and then use it as an editor. _________________ Infralution Support |
|
Back to top |
|
|
GreyCloud
Joined: 13 Nov 2008 Posts: 19
|
Posted: Tue Sep 14, 2010 10:36 am Post subject: |
|
|
Thanks for the tips: to those that come this way in the future:
Code: |
public class ButtonColumn : Column{ // uses XElement but can be changed to use your data type
private readonly Func<XElement, string> _getText;
private readonly Func<XElement, bool> _btnOnClick;
#region ctors
public ButtonColumn (Func<XElement, string> getText, Func<XElement, bool> btnOnClick) {
this._getText = getText;
this._btnOnClick = btnOnClick;
}
#endregion
#region Overrides of Column
protected override bool GetData(XElement xElement, CellData cellData) {
Button button = new Button { Text = this._getText.Invoke(xElement) };
cellData.Editor = new CellEditor(button) {
DisplayMode = CellEditorDisplayMode.Always,
ValueProperty = button.GetType().GetProperty("Tag")
};
cellData.Editor.InitializeControl += EditorInitializeControl;
return true;
}
void EditorInitializeControl(object sender, CellEditorInitializeEventArgs e) {
if (e.NewControl) {
Button button = (e.Control) as Button;
XElement element = e.CellWidget.Row.Item as XElement; // or your data type
if (button != null) {
button.Click += (clicksender, clickevents) => this._btnOnClick.Invoke(element); }
}
}
#endregion
} |
|
|
Back to top |
|
|
|