ListBoxDragDropTarget可防止ListBox填充其父控件

时间:2010-07-13 06:27:00

标签: silverlight listbox

使用Silverlight Toolkit非常容易实现基本的拖放。

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

不幸的是,似乎包装器ListBoxDragDropTarget搞砸了ListBox的正常默认行为,它将自身伸展到父控件 - 例如本例中的网格单元。

<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

3 个答案:

答案 0 :(得分:6)

ListBoxDragDropTarget派生自内容控件。只需设置Horizo​​ntalContentAlignment和 VerticalContentAlignment。

.....

答案 1 :(得分: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;
    }

答案 2 :(得分:0)

正如Archil所说,设置HorizontalContentAlignmentVerticalContentAlignment是可行的方法,但另一种方法可能是绑定WidthHeight ListBoxActualWidth的{​​{1}}和ActualHeight

ListBoxDragDropTarget
相关问题