Custom RadListView (Button item)

6 posts, 0 answers
  1. Luca
    Luca avatar
    6 posts
    Member since:
    Dec 2011

    Posted 23 Aug 2015 Link to this post

    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

  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 24 Aug 2015 Link to this post

    Hello Luca,

    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
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Luca
    Luca avatar
    6 posts
    Member since:
    Dec 2011

    Posted 24 Aug 2015 in reply to Stefan Link to this post

    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

  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 25 Aug 2015 Link to this post

    Hi Luca,

    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
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Luca
    Luca avatar
    6 posts
    Member since:
    Dec 2011

    Posted 26 Aug 2015 in reply to Stefan Link to this post

    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

  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 27 Aug 2015 Link to this post

    Hello,

    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
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready