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

select custom ListViewDataItem elements

7 Answers 495 Views
ListView
This is a migrated thread and some comments may be shown as answers.
mohammad reza
Top achievements
Rank 1
mohammad reza asked on 21 Sep 2016, 07:02 PM

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.

7 Answers, 1 is accepted

Sort by
0
Lance | Senior Manager Technical Support
Telerik team
answered on 21 Sep 2016, 07:48 PM
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
0
Accepted
Lance | Senior Manager Technical Support
Telerik team
answered on 21 Sep 2016, 08:27 PM
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
0
mohammad reza
Top achievements
Rank 1
answered on 22 Sep 2016, 07:33 AM

thanks a lot Lance.

that was very nice sample and helpful .

0
mohammad reza
Top achievements
Rank 1
answered on 22 Sep 2016, 08:53 AM

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 ????????????
}
}
0
Accepted
Lance | Senior Manager Technical Support
Telerik team
answered on 22 Sep 2016, 11:46 AM
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.
0
Victor
Top achievements
Rank 2
answered on 08 Oct 2019, 08:15 PM

Hi,

How can I access the listview-line item (and the childrenvalues) in the CheckStateChanged event?

I want to change some values of the different columns depending on the fact whether the checkbox is checked.

Hope you can help

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Oct 2019, 08:49 AM
Hello, Victor,     

In the CheckStateChanged event handler you have access to the ListViewDataItem via the Data property of the SimpleListViewVisualItem and thus affect other properties as well. Please refer to the below code snippet: 
        private void CheckBoxElement_CheckStateChanged(object sender, EventArgs e)
        {
            ListViewDataItem dataItem = this.Data;
            dataItem["Name"] = DateTime.Now.ToLongTimeString();
        }
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
ListView
Asked by
mohammad reza
Top achievements
Rank 1
Answers by
Lance | Senior Manager Technical Support
Telerik team
mohammad reza
Top achievements
Rank 1
Victor
Top achievements
Rank 2
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or