简体   繁体   中英

Silverlight Telerik RadListBox Drag and Drop not working

Hey there and thanks for looking.

Very simple.. just want to be able to reorder items in a listbox and on drop be able to change a property to track the changes. The preview drag works fine but when I drop nothing happens. Also I don't have any information in the drag drop manager OnDrag aside from the item being dragged. Any help is greatly appreciated. Thanks!

    <Style TargetType="telerik:RadListBoxItem" x:Key="draggableListBoxItem">
        <Setter Property="telerik:DragDropManager.AllowDrag" Value="True" />
        <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Margin" Value="5,0,0,0"/>
    </Style>

    <Style TargetType="telerik:RadListBox" x:Key="draggableListBox">
        <Setter Property="ItemContainerStyle" Value="{StaticResource draggableListBoxItem}" />
    </Style>

                <telerik:RadListBox Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" BorderThickness="1" Background="White" Margin="10" x:Name="OrderingBox"
                         ItemsSource="{Binding ThingsToBeOrdered}" SelectionMode="Single" AllowDrop="True" Style="{StaticResource draggableListBox}">

                    <telerik:RadListBox.DragVisualProvider>
                        <telerik:ScreenshotDragVisualProvider />
                    </telerik:RadListBox.DragVisualProvider>

                    <telerik:RadListBox.DragDropBehavior>
                        <telerik:ListBoxDragDropBehavior />
                    </telerik:RadListBox.DragDropBehavior>

                    <telerik:RadListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </telerik:RadListBox.ItemsPanel>
                    <telerik:RadListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid HorizontalAlignment="Center">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding Id}" Style="{StaticResource textBlockBoldNoMarginStyle}"/>
                                <TextBlock Grid.Column="1" Text="." Style="{StaticResource textBlockBoldNoMarginStyle}"/>
                                <Border Grid.Column="2" Padding="3,2,3,2" BorderBrush="Black" BorderThickness="1" CornerRadius="4" Margin="3,0,0,0">
                                    <Border.Background>
                                        <RadialGradientBrush>
                                            <GradientStop Color="#D9E5F3" Offset="1"/>
                                            <GradientStop Color="#F5F7F9"/>
                                        </RadialGradientBrush>
                                    </Border.Background>
                                    <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" 
                                               VerticalAlignment="Center"
                                               Foreground="#FF002F34" telerik:RadDockPanel.Dock="Top"/>
                                </Border>
                            </Grid>
                        </DataTemplate>
                    </telerik:RadListBox.ItemTemplate>
                </telerik:RadListBox>

        // -------------------------------------------------------

        DragDropManager.AddDropHandler(OrderingBox, OnDrop, true);

    private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
    {
        var items = DragDropPayloadManager.GetDataFromObject(e.Data, typeof (IdNamePair)) as List<object>;
        if (items != null && items.Any())
        {
            if (viewModel.CanWrite)
            {
                // Should only be 1 at a time, but will loop through just in case
                foreach (var draggedItem in items)
                {
                    var item = draggedItem as IdNamePair;
                    if (item != null)
                    {
                        return;
                    }
                }
            }
        }
      // If we get here, we should cancel.. somehow
    }

Finally found the answer... everything I posted was perfectly fine. The problem was I for some reason I used List rather than ObservableCollection for my ThingsToBeOrdered collection. As soon I changed that it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM