2010-07-13 77 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>