开发者

Empty WPF ListBox as Drop Target

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 ListBo开发者_StackOverflowx:

<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?


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜