This is a migrated thread and some comments may be shown as answers.

Adding RadButtonElement to RadListBoxItem

2 Answers 122 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pierre Alain
Top achievements
Rank 1
Pierre Alain asked on 01 Jan 2010, 03:16 PM
Hi,

I'm adding a RadButtonElement to a RadListBoxItem and would like it to be right aligned, but can't seem to do it.

Here is my code. The button is there, but stays on the left side.

 

privatevoid lbConnections_ItemDataBound(object sender, Telerik.WinControls.UI.ItemDataBoundEventArgs e)   
{  
    RadListBoxItem listBoxItem = (RadListBoxItem)e.DataBoundItem;  
 
    // Add button   
    RadButtonElement btn = new RadButtonElement();   
    btn.Alignment = ContentAlignment.MiddleRight;   
    btn.StretchHorizontally = false;   
    btn.StretchVertically = false;   
    btn.AutoSize = false;   
    btn.Size = new Size(15, 15);;   
    btn.Click += new EventHandler(btn_Click);   
    listBoxItem.Children.Add(btn);   
}  
 

 

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 04 Jan 2010, 12:36 PM
Hello pacarrier,

The RadButtonElement is not aligned, because RadListBoxItem is just a holder of the elements, but it does not act as a layout panel.

You can align the buttons to the right of the text by inserting the buttons in the StackLayoutPanel that holds the text. In this case the code snippet that you should use is:
void radListBox1_ItemDataBound(object sender, Telerik.WinControls.UI.ItemDataBoundEventArgs e)
{
    RadListBoxItem listBoxItem = (RadListBoxItem)e.DataBoundItem;
  
    // Add button    
    RadButtonElement btn = new RadButtonElement();
    btn.AutoSize = false;
    btn.Size = new Size(15, 15);
  
    StackLayoutPanel stackPanel = (StackLayoutPanel)listBoxItem.Children[2].Children[1];
    stackPanel.Children.Add(btn);
    stackPanel.Orientation = Orientation.Horizontal;
    stackPanel.Children[2].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;            
}

Please note that the buttons will be aligned to the right depending on the length of the text. So if the strings have different sizes, the buttons will not be positioned in one 'column'.

If you want to have the buttons in one column, this should be done with the help of the Margin property. I am attaching a sample project which demonstrates the approach.

Another option for you is to use our RadGridView as RadListBox. The column that you need in this case for the buttons is GridViewCommandColumn.

I hope this helps. If you have additional questions, feel free to contact me.

All the best,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Pierre Alain
Top achievements
Rank 1
answered on 04 Jan 2010, 01:55 PM
I finally used margin to solve my problem and handled the Resize event.

Thanks.
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Pierre Alain
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Pierre Alain
Top achievements
Rank 1
Share this question
or