Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid or border not allowing drop

I have to do a drag and drop application listView to code-generated grid. So I have done a test program and that works

enter image description here

here is the 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"/>

Now I have to migrate all this to my real application Which looks like that enter image description here

So from how the cursor looks like i can see that the drag and drop is not allowed on the grid but it is on the tiny border of each cell. So the problem is not doing d&d but ALLOWING to it

here is the xaml. The source listView is lvAllowedPPtab2, the destination grid is 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>

the grid is formed through

PalletWindow.PalletWindow.SetPalletGrid(numRows, numColumns,ref grdPalletTab2);

whose code is:

 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;
  }
}

thanks for any help Patrick

like image 563
Patrick Avatar asked Jan 06 '23 17:01

Patrick


1 Answers

To make d&d work you have to set the background of the target element (don't know why). In your case set borderImage = new SolidColorBrush(Colors.Transparent);

like image 198
Luca Avatar answered Jan 09 '23 18:01

Luca