我有一个ListBox(在XAML中定义了ItemTemplate)和一个DataGrid。 我想从DataGrid执行拖放操作到ListBox。 我的问题是我不明白如何知道哪个ListBoxItem被拖放的行已被删除。
有没有人有想法?
提前致谢。
编辑:这是ListBox的XAML:
<toolkit:DockPanel Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Width="200">
<toolkit:ListBoxDragDropTarget Name="dropTarget1" AllowDrop="True" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" AllowedSourceEffects="Move">
<ListBox Name="lbClusters">
<!-- Override default HorizontalContentAlignment -->
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<!-- Override default presentation panel (to be able to organize) -->
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<!-- Items presentation -->
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border Background="Gray" Padding="10,5,0,5" Grid.Row="0" >
<TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" />
</Border>
<ListBox ItemsSource="{Binding MatchingProcessors}" DisplayMemberPath="Name" Grid.Row="1" MinHeight="100" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</toolkit:ListBoxDragDropTarget>
</toolkit:DockPanel>
这是DataGrid:
<toolkit:DataGridDragDropTarget VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
<sdk:DataGrid Name="Grid1" SelectionChanged="Grid_SelectionChanged" AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding Configuration.Nickname}" Header="NickName" />
<sdk:DataGridTextColumn Binding="{Binding SerialNumber}" Header="SN" />
<sdk:DataGridTextColumn Binding="{Binding ComputerName}" Header="IPHostname"/>
<sdk:DataGridTextColumn Binding="{Binding Configuration.GroupName}" Header="Group" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</toolkit:DataGridDragDropTarget>
答案 0 :(得分:0)
好的,这就是我所做的......也许有更好的解决方案?但这个似乎符合我的需要。
我更改了ListBox.ItemTemplate
以在网格上添加2个事件:MouseEnter
和MouseLeave
:
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" Margin="5">
<Grid MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border Background="Gray" Padding="10,5,0,5" Grid.Row="0" >
<TextBlock Text="{Binding Name, Mode=TwoWay}" HorizontalAlignment="Stretch" />
</Border>
<ListBox ItemsSource="{Binding MatchingProcessors, Mode=TwoWay}" DisplayMemberPath="SerialNumber" Grid.Row="1" MinHeight="100" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
然后在代码中我实现了thos事件,并在ListBox中保留对hovered元素的引用。
private ListBoxItem currentListBoxItem = null;
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
List<UIElement> list = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), LayoutRoot as UIElement) as List<UIElement>;
var tmp = list.OfType<ListBoxItem>().Where(el => el.DataContext != null && el.DataContext is MyType).FirstOrDefault();
if (tmp != null)
{
this.currentListBoxItem = tmp;
}
}
private void Grid_MouseLeave(object sender, MouseEventArgs e)
{
this.currentListBoxItem = null;
}