简体   繁体   English

如何覆盖不在(xaml / wpf)中的网格上的控件

[英]How do you overlay a control over a grid that it is not in (xaml/wpf)

I have a grid that serves as a drag/drop area between textboxes, however, placing it between them causes the textboxes to be displaced. 我有一个网格用作文本框之间的拖放区域,但是,将其放在它们之间会导致文本框移位。 How can I get it so that the drag/drop grid is behind the text boxes, instead of in between them? 我如何才能使拖放网格位于文本框后面,而不是它们之间?

The first grid is the drag/drop area and below is the code for the textboxes: 第一个网格是拖放区域,下面是文本框的代码:

<Grid Grid.Column="0" Height="Auto" Width="20" AllowDrop="True" Background="White"
      DragEnter="Grid_DragEnter" DragLeave="Grid_DragLeave" Drop="Grid_Drop" Tag="{Binding}"/>

<Grid>
   <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
   </Grid.ColumnDefinitions>
   <TextBlock HorizontalAlignment="Left" Margin="5,2" 
        Text="some text" 
        VerticalAlignment="Center" FontSize="5" />
</Grid>

A picture of what I mean , where the black boxes are the textboxes, and everything enclosed in the red is the grid area where things can be dragged and dropped. 我的意思的图片 ,其中黑框是文本框,红色包围的所有内容是可以拖放对象的网格区域。

This works for me. 这对我有用。 Code behind is a mild bummer, especially referencing the droptarget by name. 后面的代码是一个温和的命令,尤其是通过名称引用droptarget。 It would be easy to turn that into an attached property that you bind to the droptarget element, which would be convenient for use in templates etc. Anyhow you can't do drag/drop without codebehind, so there you are. 将其转换为绑定到droptarget元素的附加属性将很容易,这将方便在模板等中使用。无论如何,如果没有代码隐藏,就无法进行拖放,因此就可以了。 Sometimes life gives you codebehind. 有时,生活会使您陷入困境。 Use it to, uh, make, um, lemons. 用它来做,嗯,柠檬。

A lot of this XAML is just fussing around to make the elements overlap each other the way yours do. 许多XAML只是在忙乱工作,以使元素彼此重叠,就像您所做的那样。 The Grid.Column / Grid.ColumnSpan stuff is important for the overlapping layout you have in mind. Grid.Column / Grid.ColumnSpan东西对于您要考虑的重叠布局很重要。

Note that the TextBoxes use the PreviewDragOver event, not DragOver . 请注意,TextBoxes使用PreviewDragOver事件,而不使用DragOver DragOver wasn't being raised. DragOver没有被提出。 Not sure if that's a bug or my faulty understanding, but a lot of people seem to have run into trouble getting DragOver to work with WPF TextBox . 不知道这是错误还是我的理解错误,但是很多人似乎在使DragOver与WPF TextBox一起使用时遇到麻烦。

XAML: XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>

    <Grid 
        Grid.Column="0"
        Grid.ColumnSpan="2"
        Margin="80,0,80,0"
        Height="80"
        VerticalAlignment="Center"
        HorizontalAlignment="Stretch"
        Drop="TextBox_Drop"
        DragOver="TextBox_DragOver"
        Background="DeepSkyBlue"
        x:Name="DropTargetGrid"
        AllowDrop="True"
        ></Grid>

    <TextBox 
        Grid.Column="0"
        Margin="40,40,10,40" 
        Drop="TextBox_Drop"
        PreviewDragOver="TextBox_DragOver"
        AllowDrop="True"
        VerticalAlignment="Center"
        />

    <TextBox 
        Grid.Column="1"
        Margin="10,40,40,40" 
        Drop="TextBox_Drop"
        PreviewDragOver="TextBox_DragOver"
        AllowDrop="True"
        VerticalAlignment="Center"
        />
</Grid>

Code behind: 后面的代码:

private void TextBox_Drop(object sender, DragEventArgs e)
{
    //  Do whatever
}

private void TextBox_DragOver(object sender, DragEventArgs e)
{
    var ptTargetClient = e.GetPosition(DropTargetGrid);

    if (ptTargetClient.X >= 0 && ptTargetClient.X <= DropTargetGrid.ActualWidth
        && ptTargetClient.Y >= 0 && ptTargetClient.Y <= DropTargetGrid.ActualHeight)
    {
        e.Handled = true;
        e.Effects = DragDropEffects.Move;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM