2010-07-13 60 views
2

使用Silverlight工具包可以非常轻松地启用基本拖放操作。ListBoxDragDropTarget防止ListBox填充其父控件

http://silverlightfeeds.com/post/1325/Silverlight_Toolkit_adds_DragDrop_targets.aspx

不幸的是它似乎是包装ListBoxDragDropTarget螺丝了列表框,其是本身伸展到父控制的正常默认行为 - 例如在这个例子中一个网格单元。

<Grid Background="Yellow"> 

<toolKit:ListBoxDragDropTarget AllowDrop="True"> 
     <ListBox x:Name="customerListBoxMain" 
       DisplayMemberPath="Name"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     </ListBox> 
    </toolKit:ListBoxDragDropTarget> 

</Grid> 

我会在这里结束与调整大小以适应其内容坐在一个黄色的盒子中间的小列表框(数据绑定到ListBox后)。

没有任何数量的HorizontalAlignment=Stretch等似乎能够得到它来填补父母的盒子。

我怎样才能让ListBox填写Grid

回答

0

到目前为止我所听到的最好的包装网格的大小来更改,并手动更新大小。 (我无法在XAML中工作,所以不得不使用该事件)。

<Grid Name="myListBoxWrapper" SizeChanged="myListBoxWrapper_SizeChanged">    
    <controlsToolkit:ListBoxDragDropTarget AllowDrop="True">     
    <ListBox x:Name="myListBox" > 

和在代码隐藏:

private void myListBoxWrapper_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     myListBox.Width = myListBoxWrapper.ActualWidth; 
     myListBox.Height = myListBoxWrapper.ActualHeight; 
    } 
+0

事实证明,目前的拖动功能对我来说已经不够稳定了,而且我终究还是会遇到各种奇怪的事情。 – 2010-07-16 05:02:34

6

ListBoxDragDropTarget从内容控制的。只需设置Horizo​​ntalContentAlignment和VerticalContentAlignment即可。

.....

+0

其实,th是为我工作!确保您没有在列表框上设置宽度/高度,并将垂直/水平内容对齐设置为拉伸。 – 2011-08-02 11:32:01

+0

只是偶然发现了这篇文章,而且这个解决方案也适用于我。 – 2011-09-06 04:25:49

0

正如阿尔奇尔说,设置HorizontalContentAlignmentVerticalContentAlignment是要走的路,但另一种方式很可能会绑定WidthListBoxHeightActualWidthListBoxDragDropTargetActualHeight

<toolkit:ListBoxDragDropTarget x:Name="dragdroptarget" AllowDrop="True"> 
    <ListBox x:Name="customerListBoxMain" 
     DisplayMemberPath="Name" 
      Width="{Binding ElementName=dragdroptarget, Path=ActualWidth}" 
      Height="{Binding ElementName=dragdroptarget, Path=ActualHeight}" > 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 
</toolkit:ListBoxDragDropTarget>