Custom Items in ListView do not appear

2 Answers 25 Views
ListView
Sean
Top achievements
Rank 1
Iron
Sean asked on 06 May 2022, 04:38 PM

I am using a default RadListView control, and wish to use custom list items. Following the directions here, I wrote the following class.

using System;
using System.Drawing;
using Telerik.WinControls.UI;
using Telerik.WinControls.Layouts;

namespace CustomListItemTest
{
    class SampleVisualItem : SimpleListViewVisualItem
    {
        private LightVisualElement sampleNumberField;
        private StackLayoutPanel stackPanel;

        public string sampleNumber = string.Empty;

        protected override void CreateChildElements()
        {
            base.CreateChildElements();

            stackPanel = new StackLayoutPanel
            {
                Orientation = System.Windows.Forms.Orientation.Horizontal,
                EqualChildrenWidth = true,
                ShouldHandleMouseInput = false,
                NotifyParentOnMouseInput = true
            };

            sampleNumberField = new LightVisualElement
            {
                StretchHorizontally = true,
                MinSize = new Size(50, 0),                
                ShouldHandleMouseInput = false,
                NotifyParentOnMouseInput = true                
            };
            stackPanel.Children.Add(sampleNumberField);

            Children.Add(stackPanel);
        }

        protected override void SynchronizeProperties()
        {
            base.SynchronizeProperties();

            Text = "";            
            sampleNumberField.Text = sampleNumber;
        }

        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(SimpleListViewVisualItem);
            }

        }
    }
}

 

On the form I handle these events.

private void RadForm1_Load(object sender, EventArgs e)
        {
            ListViewDataItem newItem = new ListViewDataItem();
            newItem.Key = "sampleNumber";
            newItem.Value = "123434";
            SampleList.Items.Add(newItem);
        }

        private void SampleList_VisualItemCreating(object sender, ListViewVisualItemCreatingEventArgs e)
        {
            if (SampleList.ViewType == ListViewType.ListView)
            {
                e.VisualItem = new SampleVisualItem();
            }
        }

 

However the added item does not appear. If I click around enough and move the mouse off screen, the item does appear (so it does exist), but if I click off-screen again it's gone.

What am I doing wrong?

 

2 Answers, 1 is accepted

Sort by
0
Sean
Top achievements
Rank 1
Iron
answered on 06 May 2022, 04:58 PM

As soon as I asked the question, I figured it out.

 

In the SimpleListViewVisualItem class, change:

sampleNumberField.Text = sampleNumber;

to:

sampleNumberField.Text = Data["sampleNumber"].ToString();

 

The list can't show what I don't give it.

Derp.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 May 2022, 09:32 AM
Hello, Sean, 

Following the provided information, I have prepared a sample project for your reference. The SynchronizeProperties method of the custom SimpleListViewVisualItem is the appropriate place to synchronize the LightVisualElement with the text coming from the data item: 
            protected override void SynchronizeProperties()
            {
                base.SynchronizeProperties();

                this.DrawText = false;
                sampleNumber = this.Data.Key+"";
                sampleNumberField.Text = sampleNumber;
            }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ListView
Asked by
Sean
Top achievements
Rank 1
Iron
Answers by
Sean
Top achievements
Rank 1
Iron
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or