select custom ListViewDataItem elements

6 posts, 2 answers
  1. mohammad reza
    mohammad reza avatar
    9 posts
    Member since:
    Aug 2016

    Posted 21 Sep Link to this post

    hi dears.

    i change each ListView item by this code:

    RadButtonElement buttonElement = new RadButtonElement();
            public RadCheckBoxElement checkBoxElement = new RadCheckBoxElement();       
            StackLayoutElement stack = new StackLayoutElement();
            protected override void CreateChildElements()
            {
                base.CreateChildElements();
                NotifyParentOnMouseInput = true;
                ShouldHandleMouseInput = false;
                stack.Orientation = Orientation.Vertical;
                this.MinSize = new Size(Item_MINSIZE-10,Item_MINSIZE);
     
                buttonElement.Click += ExecuteHost;
                stack.Children.Add(buttonElement);
     
                checkBoxElement.Name = "radCheckBox" + _itemIndex.ToString();
                stack.Children.Add(checkBoxElement);
                this.Children.Add(stack);
                Padding=new Padding(5,5,5,5);
            }
    and then create each item in "VisualItemCreating" event.

    so i want to change above checkbox state programmatically and by right mouse click. or call buttonElement click.

    but how can i access custom item elements objects ???

    thanks.

  2. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    135 posts

    Posted 21 Sep Link to this post

    Hi Mohammad,

    The code you shared below is not from a Windows Universal application, can you please clarify what you're using for Telerik components and what type of application this is? It appears you're referencing objects/event handlers that are part of the UI for Winforms Telerik.WinControls.UI namespace.

    I will change the thread to match the proper product, but I'd like to get confirmation before I do.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    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. Answer
    Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    135 posts

    Posted 21 Sep Link to this post

    Hello Mohammad,

    I've gone ahead and written a small sample for you that will demonstrate how to find a child element in a custom item. Defer to my code comments for the specifics of what each line does, you can run the attached demo to see this closer. Look in the debug window to see the results of checking/unchecking a checkbox.

    The custom item's CreateChildElements() method, notice the name of the RadCheckBox:

    protected override void CreateChildElements()
           {
               base.CreateChildElements();
     
               stackLayout = new StackLayoutPanel();
               stackLayout.Orientation = Orientation.Horizontal;
               stackLayout.EqualChildrenWidth = true;
               stackLayout.ShouldHandleMouseInput = false;
               stackLayout.NotifyParentOnMouseInput = true;
     
               contentElement = new LightVisualElement();
               contentElement.StretchHorizontally = true;
               contentElement.MinSize = new Size(120, 0);
               contentElement.ShouldHandleMouseInput = false;
               contentElement.NotifyParentOnMouseInput = true;
               stackLayout.Children.Add(contentElement);
     
               buttonElement1 = new RadButtonElement();
               buttonElement1.Text = "Button1";
               stackLayout.Children.Add(buttonElement1);
     
               buttonElement2 = new RadButtonElement();
               buttonElement2.Text = "Button2";
               stackLayout.Children.Add(buttonElement2);
     
               //Adding custom checkbox, note you do not need to name the item using the item's index
               checkBoxElement = new RadCheckBoxElement();
               checkBoxElement.Name = "CheckBox1";
               stackLayout.Children.Add(checkBoxElement);
     
               Children.Add(stackLayout);
           }


    Here's the ListView's VisualItemCreating handler and the reusable CheckStateChanged event handler:

    private void RadListView1_VisualItemCreating(object sender, ListViewVisualItemCreatingEventArgs e)
            {
                if (radListView1.ViewType == ListViewType.ListView)
                {
                    var visualItem = new MyCustomVisualItem();
     
                    // you can find the child you're looking for with this helper method
                    var cb = visualItem.ChildrenHierarchy.FirstOrDefault(c => c.Name == "CheckBox1");
     
                    //confirm it's indeed what you want
                    if (cb is RadCheckBoxElement)
                    {
                        //then make on demand changes as you see fit
                        ((RadCheckBoxElement)cb).CheckStateChanged += RadCheckBox_CheckStateChanged;
                    }
                     
                    //assign the custom item as the RadListView visual item
                    e.VisualItem = visualItem;
                }
            }
     
            private void RadCheckBox_CheckStateChanged(object sender, EventArgs e)
            {
                Debug.WriteLine($"ItemCheckBox changed- Checked: {((RadCheckBoxElement)sender).Checked}");
            }

    Note that even though I use LINQ to get the RadCheckboxElement, you can use your preferred approach. The key is that all the elements will be in the flat list returned by the ChildrenHierarchy property.

    Let us know if you have any further trouble.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    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
  5. mohammad reza
    mohammad reza avatar
    9 posts
    Member since:
    Aug 2016

    Posted 22 Sep in reply to Lance | Tech Support Engineer, Sr. Link to this post

    thanks a lot Lance.

    that was very nice sample and helpful .

  6. mohammad reza
    mohammad reza avatar
    9 posts
    Member since:
    Aug 2016

    Posted 22 Sep in reply to Lance | Tech Support Engineer, Sr. Link to this post

    but now i have another problem to inform what item index i right mouse clicked on it?

    private void radListViewHosts_MouseDown(object sender, MouseEventArgs e)
     
    {
     
    if (e.Button == MouseButtons.Right)
    {
          var item = radListViewHosts.ElementTree.GetElementAtPoint(e.Location).parent;
          // how to get listview item index here ????????????
    }
    }
  7. Answer
    Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    135 posts

    Posted 22 Sep Link to this post

    Hi Mohammad,

    This question is off-topic, therefore i have created a new forum thread for you. Please go here to open the forum thread and see my answer (with code).

    In the future, please open a new thread if the question is of a different topic. That way you can get the help of the specific engineers responsible for that area of expertise. If you need to share previously posted code, you can always link to the initial thread via URL (like i did for you here).

    Thank you for your understanding and thank you for contacting Telerik Support.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready