ListBoxDropTarget-如何获取放置位置索引? [英] ListBoxDropTarget - How do I get a drop location index?
问题描述
好,
我整天都在战斗-无济于事...
我在Silverlight中有一个ListBox,我想赋予用户通过拖放进行重新排序的能力.所以我想我会很聪明,并利用Microsoft Silverlight工具箱ListBoxDropTarget控件来实现魔术.
基本上,除了确定在哪里放置项目的索引(它在同一listBox中被重新排序)之外,我都能按照我期望的方式工作.
如何生成(或捕获)放置位置/(的索引)以对列表框重新排序?
样品-会很有帮助...
这是XAML:
Ok,
I have been fight''n this all day - to no avail...
I have a ListBox in Silverlight that I would like to give the user the ability to reorder via drag and drop.. So I thought I would be smart and leverage the Microsoft Silverlight toolkit ListBoxDropTarget control to do the magic.
Basically, I have everything working the way I would expect except for identifying the index where to drop the item (It''s being reordered in the same listBox).
How can I generate (or capture) the drop location / (index of) to reorder the listbox?
Samples - would be helpful...
Here is the XAML:
<toolkit:ListBoxDragDropTarget x:Name="dropTarget" AllowDrop="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ItemDragCompleted">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ReOrderCommand}" CommandParameter="{Binding ElementName=dropTarget.ItemDragEventArgs}" PassEventArgsToCommand="True"></GalaSoft_MvvmLight_Command:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox x:Name="listQueries" AllowDrop="True" ItemsSource="{Binding AllQueries}" ScrollViewer.VerticalScrollBarVisibility="Auto" Style="{StaticResource stlListBox}" Height="Auto" Width="363">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<CheckBox IsChecked="{Binding IsVisible}" ToolTipService.ToolTip="Check/Uncheck to show/hide this queries results" VerticalAlignment="Top"></CheckBox>
<Button Content="+" Command="{Binding ShowQueryCommand}" VerticalAlignment="Top" />
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding QueryName}" />
<StackPanel Visibility="{Binding IsOpen,Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}}" >
<TextBlock Text="{Binding QueryStringForPanel}" TextWrapping="Wrap" />
</StackPanel>
</StackPanel>
<Button x:Name="btnDelete" Content="X" Style="{StaticResource stlListBoxDeleteButton}"
Command="{Binding Path=DataContext.DeleteQueryCommand, ElementName=listQueries}" CommandParameter="{Binding}" HorizontalAlignment="Right" VerticalAlignment="Top">
</Button>
<UC:ComboColorpicker Grid.Column="1" Grid.Row="1" selectedColor="{Binding HightlightColour, Mode=TwoWay}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</toolkit:ListBoxDragDropTarget>
//And here is the code from my ViewModel:
private void ReOrder(System.Windows.Controls.ItemDragEventArgs LBDDT)
{
//**** --- how do we get the index -- for the drop location????
int myIndx = 0;
var tmp = (LBDDT.DragSource as System.Windows.Controls.ListBox).ItemsSource.Cast<MyClass>().ToList();
SelectionCollection selCollection = LBDDT.Data as SelectionCollection;
MyClass qvm = selCollection[0].Item as MyClass;
//**** get current index
int idx = tmp.IndexOf(qvm);
//**** if I manually set the index -- it works fine... --Just need to understand how to dynamically generate the drop location index
myIndx = idx - 1;
//**** AllResults is the collection of MyClass that the LsitBox is bound to... --Again - this works if I hard code the drop index... How do I get the drop Index location for reordering?
AllResults.Remove(qvm);
AllResults.Insert(myIndx, qvm);
}
推荐答案
似乎工具箱ListBoxDragDropControl使用ListBox.ItemsPanel来放置" ...如果您将ListBox.ItemTemplate与DataTemplate一起使用您必须添加ListBox.ItemsPanel才能使控件正常工作.
Appears that the toolkit ListBoxDragDropControl uses a ListBox.ItemsPanel to "drop"... If you are using a ListBox.ItemTemplate with a DataTemplate you must add a ListBox.ItemsPanel for the control to work.
<ListBox x:Name="myListBox" AllowDrop="True" ItemsSource="{Binding BindSource}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate> MY-DATA - STUFF </DataTemplate>
<ListBox.ItemTemplate>
</ListBox>
这篇关于ListBoxDropTarget-如何获取放置位置索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!