在Silverlight中拖动事件不会为ListBox触发

时间:2012-05-25 03:39:04

标签: c# .net silverlight-toolkit silverlight-5.0

希望这是一个简单的问题。我使用Silverlight Toolkit中的ListBoxDragDropTarget来设置从一个ListBox拖放到另一个<toolkit:ListBoxDragDropTarget HorizontalAlignment="Left" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" VerticalContentAlignment="Stretch" Margin="39,117,0,0" Grid.Row="1" AllowDrop='True'> <ListBox x:Name='columnHeadings' MinHeight='100' MinWidth='100'> </ListBox> </toolkit:ListBoxDragDropTarget> <toolkit:ListBoxDragDropTarget AllowDrop='True' Grid.Column='1' Grid.Row='1' HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" VerticalAlignment="Center" HorizontalAlignment="Left"> <ListBox x:Name='customerFields' Grid.Column='1' Grid.Row='1' Visibility='Collapsed' Drop='CustomerFieldsDrop'> </ListBox> </toolkit:ListBoxDragDropTarget> 。我似乎无法让事件发生。这是我的XAML代码:

private void CustomerFieldsDrop(object sender, DragEventArgs e)
{
  MessageBox.Show(string.Format("Something was dropped!"));
}

这里是代码隐藏页面中的事件处理程序:

ListBoxDragDropTarget

正如你所看到的,我的目标是让事情变得简单。我尝试为customerFields列表框的父columnHeadings.ItemsSource分配事件处理程序;具有讽刺意味的是,这很有效。

我的目的是允许用户导入文本文件并在一个列表框中获取文件列标题的列表,然后&#34; connect&#34;它们到第二个列表框中列出的数据字段。因此,没有列表重新排序或将项目从一个列表移动到另一个列表。

string[]属性是customerFields.ItemsSource对象。 IEnumerable<string>属性是{{1}}对象。

任何见解都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我认为AllowDrop =“True”和Drop =“EventName”属性需要在同一元素内才能工作。您可能必须在ListBox本身中将AllowDrop属性设置为“True”:

<ListBox x:Name="customerFields" 
    Grid.Column="1" 
    Grid.Row="1" 
    Visibility="Collapsed"
    Drop="CustomerFieldsDrop"
    AllowDrop="True" 
</ListBox> 

或者将Drop =“CustomerFieldsDrop”属性添加到ListBoxDragDropTarget标记:

<toolkit:ListBoxDragDropTarget AllowDrop='True'   
    Grid.Column='1'   
    Grid.Row='1'   
    HorizontalContentAlignment="Stretch"   
    VerticalContentAlignment="Stretch"   
    VerticalAlignment="Center"   
    HorizontalAlignment="Left"
    Drop="CustomerFieldsDrop">

任何一个都应该工作......

相关问题