Completed
Last Updated: 27 Jan 2015 13:06 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 01 Dec 2014 13:25
Category: ListView, CardView and CheckedListBox
Type: Bug Report
2
FIX. RadListView - NullReferenceException when dragging a custom DetailListViewVisualItem
To reproduce:

public Form1()
{
    InitializeComponent();

    this.radListView1.AllowDragDrop = true;
    this.radListView1.ViewType = ListViewType.DetailsView;
    this.radListView1.ItemSize = new System.Drawing.Size(120, 40);

    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Image", typeof(Image));
    for (int i = 0; i < 3; i++)
    {
        dt.Rows.Add(i, "Item" + i, Properties.Resources._2RibbonMenuOpen2);
    }
    foreach (DataRow row in dt.Rows)
    {
        ListViewDataItem di = new ListViewDataItem();
        di.DataBoundItem = row;
        this.radListView1.Items.Add(di);
    }
}

private void radListView1_VisualItemCreating(object sender, ListViewVisualItemCreatingEventArgs e)
{
    e.VisualItem = new CustomItem();
}

public class CustomItem : DetailListViewVisualItem
{
    private DockLayoutPanel panel;
    private LightVisualElement imageElement;
    private LightVisualElement titleElement;

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

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

        panel = new DockLayoutPanel();
        this.Children.Add(panel);
       
        imageElement = new LightVisualElement();
        imageElement.Size = new System.Drawing.Size(25, 40);
        panel.Children.Add(imageElement);
        DockLayoutPanel.SetDock(imageElement, Telerik.WinControls.Layouts.Dock.Left);
        
        titleElement = new LightVisualElement();
        panel.Children.Add(titleElement);
        titleElement.Size = new System.Drawing.Size(55, 40);

        titleElement.ShouldHandleMouseInput = false;

        DockLayoutPanel.SetDock(titleElement, Telerik.WinControls.Layouts.Dock.Right);
    }

    protected override SizeF MeasureOverride(SizeF availableSize)
    {
        SizeF measuredSize = base.MeasureOverride(availableSize);
        this.panel.Measure(measuredSize);
        return measuredSize;
    }

    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        base.ArrangeOverride(finalSize);
        this.panel.Arrange(new RectangleF(PointF.Empty, finalSize));

        return finalSize;
    }

    protected override void SynchronizeProperties()
    {
        base.SynchronizeProperties();
        this.Text = string.Empty;
        DataRow row = this.Data.DataBoundItem as DataRow;
        if (dataItem != null)
        {
            this.imageElement.Image = row["Image"] as Image;
            this.titleElement.Text = row["Name"].ToString();
        }
    }
}


Workaround:

 this.radListView1.ListViewElement.DragDropService = new CustomService(this.radListView1.ListViewElement);

public class CustomService : ListViewDragDropService
{ 
    public CustomService(RadListViewElement owner) : base(owner)
    {
    }

    protected override void UpdateDragHintLocation(Point mousePosition)
    {
        FieldInfo fi = typeof(ListViewDragDropService).GetField("dragHintWindow", System.Reflection.BindingFlags.Instance
            | System.Reflection.BindingFlags.NonPublic);
        if (fi != null)
        {
            RadLayeredWindow dragHintWindow = fi.GetValue(this) as RadLayeredWindow;
            if (dragHintWindow == null)
            {
                return;
            }
        }
        base.UpdateDragHintLocation(mousePosition);
    }
}
Attached Files:
0 comments