将 TreeViewItem 拖放到 TreeView 的不可点击区域 [WPF]
关于在 WPF 中拖放树视图项的问题。
我原来的问题有点复杂。所以我简化了它,代码如下: XAML
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TreeView>
<TreeView.ItemContainerStyle>
<Style>
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
<Setter Property="TreeViewItem.Background" Value="LightBlue"/>
<Setter Property="TreeViewItem.AllowDrop" Value="True"/>
<EventSetter Event="TreeViewItem.MouseMove" Handler="TreeNode_MouseMove"/>
<EventSetter Event="TreeViewItem.Drop" Handler="TreeNode_Drop"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeViewItem Header="first node in the tree"/>
<TreeViewItem Header="second node in the tree"></TreeViewItem>
<TreeViewItem Header="third node in the tree"></TreeViewItem>
</TreeView>
</Grid>
和代码隐藏:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void TreeNode_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(this, DateTime.Now.ToString(), DragDropEffects.Move);
}
}
private void TreeNode_Drop(object sender, DragEventArgs e)
{
string str = (string)e.Data.GetData(typeof(string));
MessageBox.Show(str);
}
}
所以我想要做的很简单,只需将一个树视图项拖放到另一个树视图项上时弹出一个消息框即可。
当我将它放在另一个项目上时,它工作正常,如下所示:
但如果我稍微拖动它超出项目的边界,它不起作用,如下所示:
鼠标光标显示为“禁止标志”,抱歉我无法在屏幕截图中看到它。
所以现在我的问题是:如何使第二个条件成立?当放置位置稍微偏离项目边界时,如何使 Drop 事件仍然触发?
提前致谢。
A question about dragging and dropping tree view items in WPF.
My original question is a little complex. So I simplified it, and here is the code:
XAML
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TreeView>
<TreeView.ItemContainerStyle>
<Style>
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
<Setter Property="TreeViewItem.Background" Value="LightBlue"/>
<Setter Property="TreeViewItem.AllowDrop" Value="True"/>
<EventSetter Event="TreeViewItem.MouseMove" Handler="TreeNode_MouseMove"/>
<EventSetter Event="TreeViewItem.Drop" Handler="TreeNode_Drop"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeViewItem Header="first node in the tree"/>
<TreeViewItem Header="second node in the tree"></TreeViewItem>
<TreeViewItem Header="third node in the tree"></TreeViewItem>
</TreeView>
</Grid>
And code-behind:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void TreeNode_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(this, DateTime.Now.ToString(), DragDropEffects.Move);
}
}
private void TreeNode_Drop(object sender, DragEventArgs e)
{
string str = (string)e.Data.GetData(typeof(string));
MessageBox.Show(str);
}
}
So what I want to do is very simple, just pop up a message box when one tree view item is dragged and dropped on another tree view item.
When I drop it right on another item, it works fine, like this:
But if I drag it slightly off the boundary of the item, it does not work, like this:
The mouse cursor is displayed as a “forbidden sign”, sorry I can’t get it in the screen shot.
So now my question is: how to make the second condition work? How to make the Drop event still fire up when the dropping location is slightly off the boundary of the item?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
采用
TreeViewItem
的默认ControlTemplate
并将Border
(包含 ContentPresenter 的边框)设置为 2 的 colspan,例如: TreeViewItem 中 TextBlock 旁边的整个区域将变得可点击。您可能想尝试一下该模板以适应您的需求。
下面是使用 转储控制模板实用程序。
Take the default
ControlTemplate
ofTreeViewItem
and make theBorder
(the border which contains the ContentPresenter) to have a colspan of 2, e.g.:Note that the whole area in TreeViewItem next to the TextBlock would become clickable. You may want to play around with the template to fit it to your needs.
Below is the ControlTemplate of TreeViewItem extracted using Dump Control Template Utility.
对 TreeView 本身使用 AllowDrop 属性和 Drop 事件。
这是来自msdn:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4b8bafe5-ae3e-439d-953a-f534a60dbb2d/
use AllowDrop property and Drop event for TreeView itself.
this is from msdn: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4b8bafe5-ae3e-439d-953a-f534a60dbb2d/