Hello,
I've created a custom RadListView following this page: Custom Items | UI for WinForms
However when I click a ButtonElement in RadListView, the correspondent ListViewItem is selected. How can I avoid this?
See also "image.png".
Thanks in advance,
Luca Scalzotto
5 Answers, 1 is accepted
Thank you for writing.
To prevent the change in the selection, in the MouseDown event of the button in the custom cell, you should set the control element Tag with some value, so later you would know whether the selection request comes from the button or not:
private
void
Button_MouseDown(
object
sender, MouseEventArgs e)
{
this
.RowElement.CellsContainer.Context.Tag =
"ButtonIsClicked"
;
}
Then you should create a descendant of RadListView and override the OnMouseUp property and return if the Tag is filled with your value:
class
MyRadListView : RadListView
{
protected
override
void
OnMouseUp(MouseEventArgs e)
{
if
(
this
.ListViewElement.ViewElement.Tag +
""
==
"ButtonIsClicked"
)
{
this
.ListViewElement.ViewElement.Tag =
null
;
return
;
}
base
.OnMouseUp(e);
}
public
override
string
ThemeClassName
{
get
{
return
typeof
(RadListView).FullName;
}
}
}
Here is a whole example you can test with:
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
radListView1 =
new
MyRadListView();
radListView1.Parent =
this
;
radListView1.Dock = DockStyle.Fill;
radListView1.CellCreating += radListView1_CellCreating;
radListView1.ViewType = ListViewType.DetailsView;
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Id"
,
typeof
(
int
));
dt.Columns.Add(
"Name"
,
typeof
(
string
));
for
(
int
i = 0; i < 50; i++)
{
dt.Rows.Add(i,
"Item "
+ i);
}
this
.radListView1.DataSource = dt;
}
private
void
radListView1_CellCreating(
object
sender, ListViewCellElementCreatingEventArgs e)
{
DetailListViewDataCellElement cell = e.CellElement
as
DetailListViewDataCellElement;
if
(cell !=
null
&& cell.Data.Name ==
"Name"
)
{
e.CellElement =
new
CustomDetailListViewDataCellElement(cell.RowElement, e.CellElement.Data);
}
}
class
MyRadListView : RadListView
{
protected
override
void
OnMouseUp(MouseEventArgs e)
{
if
(
this
.ListViewElement.ViewElement.Tag +
""
==
"ButtonIsClicked"
)
{
this
.ListViewElement.ViewElement.Tag =
null
;
return
;
}
base
.OnMouseUp(e);
}
public
override
string
ThemeClassName
{
get
{
return
typeof
(RadListView).FullName;
}
}
}
public
class
CustomDetailListViewDataCellElement : DetailListViewDataCellElement
{
private
RadButtonElement button;
public
CustomDetailListViewDataCellElement(DetailListViewVisualItem owner,
ListViewDetailColumn column) :
base
(owner, column)
{
}
protected
override
void
CreateChildElements()
{
base
.CreateChildElements();
this
.button =
new
RadButtonElement();
button.MouseDown += Button_MouseDown;
this
.Children.Add(
this
.button);
}
private
void
Button_MouseDown(
object
sender, MouseEventArgs e)
{
this
.RowElement.CellsContainer.Context.Tag =
"ButtonIsClicked"
;
}
protected
override
Type ThemeEffectiveType
{
get
{
return
typeof
(DetailListViewHeaderCellElement);
}
}
public
override
void
Synchronize()
{
base
.Synchronize();
this
.Text =
""
;
DataRowView rowView =
this
.Row.DataBoundItem
as
DataRowView;
this
.button.Text = rowView.Row[
"Name"
].ToString();
}
public
override
bool
IsCompatible(ListViewDetailColumn data,
object
context)
{
if
(data.Name !=
"Name"
)
{
return
false
;
}
return
base
.IsCompatible(data, context);
}
}
I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.
Regards,
Stefan
Telerik
Hello Stefan,
I tried your code but Button does not show MouseUp event effect (the button remains 'pressed'). Is there the possibility to handle only Button events - MouseUp, MouseDown, ecc - (when I click on an item in listview that is not Button nothing happens, ListviewDataItem can't be selected).
Sorry for bad English,
Thanks in advance,
Luca Scalzotto
On my end the button does not remain clicked with the code I've provided. Do you have something additional?
I am using the default theme, which theme are you using? Also, which version of the suite are you using?
Regards,
Stefan
Telerik
Hi Stefan,
I was using Windows8 theme, version 2015.2.623.40. I resolved with this code:
Private Sub MyRadListView1_VisualItemCreating(sender As Object, e As ListViewVisualItemCreatingEventArgs) Handles MyRadListView1.VisualItemCreating
e.VisualItem.DrawBorder = False
e.VisualItem.DrawFill = False
End Sub
In this way, selection disappears, but I can still select RadButtonElement. Thank you very much for answers​.
Regards,
Luca Scalzotto
I am glad you have solved this.
Just to mention that a more proper place to set these properties would be the CellFormatting event, not the VisualItemCreating.
Regards,
Stefan
Telerik