简体   繁体   English

WPF边框属性无法正常工作

[英]WPF Border Property not working correctly

I am working on an application that is using Drag and Drop functionality. 我正在使用拖放功能的应用程序。 When I drag certain items over other items, I would like to show if they are towards the top, or the bottom of the item by setting the border property of the item they are over dynamically(as oppose to in the xaml). 当我将某些项目拖到其他项目上时,我想通过设置它们动态结束的项目的border属性来显示它们是朝向项目的顶部还是底部(与xaml中的相反)。

I have looked into this, and used examples, but none of them are working. 我已经研究了这个问题,并使用了示例,但是它们都没有起作用。 Here is the code I have that is called when a drag operation is performed over the Grid Item. 这是在网格项上执行拖动操作时调用的代码。

 private void Grid_DragEnter(object sender, DragEventArgs e)
    {
        Grid grid = (Grid)sender;
        grid.Background = Brushes.Cornsilk;

        Border border = new Border();
        border.BorderBrush = Brushes.Black;
        border.BorderThickness = new Thickness(5, 10, 15, 20);
        border.Background = Brushes.Black;
        border.Padding = new Thickness(10);
        border.Child = grid;
    }

The code above is only test code so that I can see the border is showing. 上面的代码只是测试代码,因此我可以看到边框显示。 Once I can get this, then I will set the top or bottom border at separate times, depending on if they are above or below the center of the grid item. 一旦获得此权限,我将在单独的时间设置顶部或底部边框,具体取决于它们是在网格项目中心之上还是之下。

You're setting the border's child, but the border itself needs to be added to some container in order for it to be visible. 您正在设置边框的子级,但是边框本身需要添加到某个容器中才能显示。

That being said, you might want to look into Adorners . 话虽如此,您可能想研究Adorners They are designed for exactly this scenario, and don't require changing the visual hierarchy of your UI at runtime. 它们正是针对这种情况而设计的,不需要在运行时更改UI的可视层次结构。

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

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