网格或边框不允许丢弃

时间:2016-01-30 19:13:25

标签: c# wpf listview drag-and-drop grid

我必须将应用程序li​​stView拖放到代码生成的网格中。 所以我做了一个测试程序并且有效。

enter image description here

这是xaml

<Grid Margin="0,0,-61.6,0.4">
<ListView x:Name="lwOne"  PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"  Background="Bisque" Margin="16,65,340.2,22">
  <ListView.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel Orientation="Horizontal" Width="250" VerticalAlignment="Top"></WrapPanel>
    </ItemsPanelTemplate>
  </ListView.ItemsPanel>

</ListView>

<Grid Name="grdMain" Drop="Grid_Drop" Background="AliceBlue" AllowDrop="True" Margin="380,65,81.2,62"/>

现在我必须将所有这些迁移到我的实际应用程序中 enter image description here

因此,从光标的外观来看,我可以看到网格上不允许拖放,但它位于每个单元格的微小边框上。 所以问题不在于d&amp; d但是允许它

这是xaml。源listView是lvAllowedPPtab2,目标网格是grdPalletTab2

<TabItem Name="tabItem2" HorizontalAlignment="Center" Height="80" MouseLeftButtonUp="TabItem_MouseLeftButtonUp"  FontSize="{StaticResource TOOLTIP_FONTSIZE}" IsSelected="false" >
    <TabItem.Header>
      <StackPanel>
        <TextBlock Text="&#xF0F7;" FontSize="{StaticResource TAB_FONTSIZE}"/>
        <TextBlock Name="tbTab2" Visibility="Hidden" FontSize="{StaticResource BUTTON_FONTSIZE}" />
      </StackPanel>
    </TabItem.Header>
    <TabItem.Background>
      <ImageBrush/>
    </TabItem.Background>
    <Grid >
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Border x:Name="Border1Tab2" BorderBrush="Gainsboro" BorderThickness="5" Width="200" Margin="10,10,10,10" >
        <StackPanel Margin="-1.8,-0.8,2.2,1.4">
          <ListBox x:Name="lbxPalletsTab2" Background="{x:Null}" BorderBrush="{x:Null}" Height="600" SelectionChanged="ListBox_SelectionChanged" Margin="12,10.2,8.4,10.4" />
        </StackPanel>
      </Border>
      <Border x:Name="Border2Tab2" BorderBrush="Gainsboro" MinWidth="150" BorderThickness="5" Grid.Column="1" Margin="10,10,10,10">
        <Grid >
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300px"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>

          <Grid  HorizontalAlignment="Stretch" Grid.Column="0">
            <Grid.RowDefinitions>
              <RowDefinition Height="50px"/>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Name="tbkPPtab2" Grid.Row="0" FontSize="22" Background="{x:Null}" FontWeight="Black" Text="---" HorizontalAlignment="Center" VerticalAlignment="Bottom"></TextBlock>
            <ListView x:Name="lvAllowedPPtab2" Grid.Row="1" FontSize="12" Background="{x:Null}"  BorderBrush="Gainsboro" BorderThickness="5" Margin="10" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown">
              <ListView.ItemsPanel >
                <ItemsPanelTemplate >
                  <WrapPanel Orientation="Horizontal" Width="250" Background="{x:Null}"  VerticalAlignment="Top"></WrapPanel>
                </ItemsPanelTemplate>
              </ListView.ItemsPanel>
            </ListView>

          </Grid>
          <Border  Grid.Column="1" BorderBrush="Gainsboro"  BorderThickness="5" Margin="10,60,10,10">
            <Grid Name="grdPalletTab2" AllowDrop="True" Drop="Grid_Drop"/>
          </Border>
        </Grid>
      </Border>
    </Grid>
  </TabItem>

网格是通过

形成的
PalletWindow.PalletWindow.SetPalletGrid(numRows, numColumns,ref grdPalletTab2);

其代码为:

 public static bool SetPalletGrid(int numRows, int numColumns, ref Grid grd)
{
  try
  {
    grd.Children.Clear();
    grd.RowDefinitions.Clear();
    grd.ColumnDefinitions.Clear();
    grd.AllowDrop = true;
    for (int row = 0; row < numRows; row++)
    {
      var rd = new RowDefinition();
      rd.AllowDrop = true;
      rd.Height = new GridLength(1.0, GridUnitType.Star);
      grd.RowDefinitions.Add(rd);
    }

    for (int column = 0; column < numColumns; column++)
    {
      var cd = new ColumnDefinition();
      cd.AllowDrop = true;
      cd.Width = new GridLength(1.0, GridUnitType.Star);
      grd.ColumnDefinitions.Add(cd);
    }

    for (int row = 0; row < numRows; row++)
    {
      for (int column = 0; column < numColumns; column++)
      {
        var borderImage = new Border();
        borderImage.AllowDrop = true;
        borderImage.BorderThickness = new Thickness(2);
        borderImage.BorderBrush = new SolidColorBrush(Colors.Black);
        borderImage.Name = "BRD_" + row + "_" + column;
        borderImage.Effect = new DropShadowEffect
        {
          Color = new Color { R = 255, G = 255, B = 255 },
          Direction = 320,
          ShadowDepth = 5,
          Opacity = 0.95
        };

        Grid.SetRow(borderImage, row);
        Grid.SetColumn(borderImage, column);
        grd.Children.Add(borderImage);
      }
    }
    return true;
  }
  catch// (Exception exc)
  {
    return false;
  }
}

感谢您的帮助 帕特里克

1 个答案:

答案 0 :(得分:7)

要使d&amp; d工作,你必须设置目标元素的背景(不知道为什么)。在你的情况下设置borderImage = new SolidColorBrush(Colors.Transparent);