Search code examples
c#wpftelerik

RadListBox vs RadListBoxItem DragDrop WPF


I'm working on a WPF application with MVVM pattern using Telerik controls.

I'm using telerik:RadListBox for which a collection is bind. My question is when a collection is bound to a RadListBox the collection will be turned as RadListBox or RadListBoxItem ?

XAML:

<telerik:RadListBox x:Name="lstMarketSeries" ItemsSource="{Binding MarketSeriesCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, ValidatesOnDataErrors=True}" ItemContainerStyle="{StaticResource DraggableListBoxItem}"DragLeave="lstMarketSeries_DragLeave" Style="{StaticResource myListboxStyle}" SelectionMode="Extended" telerik:StyleManager.Theme="Windows8">
</telerik:RadListBox>

XAML.cs:

/// <summary>
/// Event fired when series are dragged and moved.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lstMarketSeries_DragLeave(object sender, DragEventArgs e)
{

   if (sender is RadListBox) // True
    {

    }

   if(sender is RadListBoxItem) //False Why??
    {

    }
}

When a collection is bound it should be returning as Items Correct? Why am i getting it as RadListBox itself and not RadListBoxItem ?

Now i need that Draggable object as RadListBoxItem. Is there any way?

FYI,

See here ListBoxDragDrop the sender is ListBoxItem and not ListBox


Solution

  • See the docs.

    sender is

    The object where the event handler is attached.

    That's it.

    You are dragging an item, but the event is attached to the container.

    <telerik:RadListBox ... DragLeave="lstMarketSeries_DragLeave" ... >`
    

    Therfore when DragLeave event occurs to the RadListBox, the specified handler is fired (which is actually set by you). sender is set to be the RadListBox because the handler is attached to it, not the item.

    You have misunderstood the example you're referring to. Read the code of the example carefully. In the example event handler is set to the ItemContainerStyle , which is used to inflate every item. In the example we see the handler, attached to each item individually, therefore sender of the handler is set to be an item rather than the control itself.

    If you need sender to de an item you have to do the same - create a style and attach a handler to it.