2

I have a ListBox that is a drop target of items from other sources.

Everything is working fine except in a particularly situation. When the ListBox has no Items I can only drop in the border of ListBox (I have a trigger so the Border is visible when dragging).

To give a bigger drop area I set the MinHeight of the ListBox to 25. When dragging, the Border reflects the MinHeight of the ListBox but the area is not considered a target. What is probably happening is that the target is considered to be the background because there is no Item in the ListBox.

Here is the code for the ListBox:

<ListBox Name="itmCtrlSetupSteps" Grid.Row="1" MinHeight="25"
         BorderThickness="2" BorderBrush="{Binding DropBrush}" Background="Transparent"
         ItemsSource="{Binding SetupSteps}" SelectionMode="Single" ItemContainerStyle="{StaticResource StepItemStyle}"
         HorizontalContentAlignment="Stretch" Focusable="True"
         SelectionChanged="manageStep_SelectionChanged"
         AllowDrop="True" DragOver="itmCtrls_DragOver" Drop="itmCtrls_Drop" KeyUp="List_KeyUp"
         >
    <ListBox.Template>
        <ControlTemplate TargetType="ListBox">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                <ItemsPresenter/>
            </Border>
        </ControlTemplate>
    </ListBox.Template>
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type my:TestStepListingStepViewModel}">
            <my:TestStepListingStepView HorizontalAlignment="Stretch" GotFocus="setupSteps_GotFocus" MouseDoubleClick="Step_MouseDoubleClick"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

If I set the ItemPanel to:

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <Grid ClipToBounds="True"/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

I can drop items in the empty ListBox but then the items are presented on top of each other, instead of as a list. Any thoughts on this?

2 Answers 2

4

The problem is that your ListBox isn't showing up when it is hit tested. You need to set the Background Brush on the Border in the control template so that it reflects your setting of Transparent on the ListBox.

<ControlTemplate TargetType="ListBox">
    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
            <ItemsPresenter/>
    </Border>
</ControlTemplate>
Sign up to request clarification or add additional context in comments.

Comments

3

In case someone is having this issue with any other control, just surround it with a border, set the background to a colour, and add the drag/drop events on the border along with AllowDrop set to be true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.