Get the SurfaceListBoxItem and ScatterViewItem when object is dropped on a SurfaceListBox and ScatterView

StackOverflow https://stackoverflow.com/questions/14071389

  •  12-12-2021
  •  | 
  •  

Pergunta

I'm dropping an object (ScatterViewItem) on a SurfaceListBox with the s:SurfaceDragDrop event, and it works fine when detecting the dropping on the whole SurfaceListBox, however, I want to know in which SurfaceListBoxItem the object was dropped.

I also want to do this but for a ScatterView, i.e., detecting which ScatterViewItem of that ScatterView the object was dropped on.

My code is something like this:

<s:SurfaceListBox 
    x:Name="listBoxList"
    Background="{x:Null}"
    AllowDrop="True"
    s:SurfaceDragDrop.Drop="ListBox_Drop" >
</s:SurfaceListBox>

<s:ScatterView 
    x:Name="scatterList"
    Background="{x:Null}"
    AllowDrop="True"
    s:SurfaceDragDrop.Drop="Scatter_Drop" >
</s:ScatterView>

And then I add my items:

listBoxList.Items.Add("ListBox Item 1");
listBoxList.Items.Add("ListBox Item 1");
listBoxList.Items.Add("ListBox Item 1");

scatterList.Items.Add("ScatterViewItem A");
scatterList.Items.Add("ScatterViewItem B");
scatterList.Items.Add("ScatterViewItem C");

So how can I get the item on the ListBox_Drop and Scatter_Drop?

EDIT

Through Robert answer I managed to solve my problem. So the resulting code would be something like this (for the ScatterView):

<s:ScatterView
    x:Name="scatterList"
    Background="{x:Null}">
    <s:ScatterView.ItemContainerStyle>
        <Style TargetType="s:ScatterViewItem">
            <EventSetter Event="s:SurfaceDragDrop.Drop" Handler="Scatter_Drop"/>
            <Setter Property="AllowDrop" Value="True" />
        </Style>
    </s:ScatterView.ItemContainerStyle>
</s:ScatterView>

And for the SurfaceListBox:

<s:SurfaceListBox
    x:Name="listBoxList"
    Background="{x:Null}">
    <s:SurfaceListBox.ItemContainerStyle>
        <Style TargetType="s:SurfaceListBox">
            <EventSetter Event="s:SurfaceDragDrop.Drop" Handler="ListBox_Drop"/>
            <Setter Property="AllowDrop" Value="True" />
        </Style>
    </s:SurfaceListBox.ItemContainerStyle>
</s:SurfaceListBox>
Foi útil?

Solução

You need to set AllowDrop and hook up your Drop event handler for each individual ScatterViewItem & ListBoxItem. Then the source of the event will be the item that got dropped upon.

Outras dicas

A small correction I think to the above solution.

<Style TargetType="s:SurfaceListBoxItem">

as oppose to

<Style TargetType="s:SurfaceListBox">
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top