在TreeView [WPF]的不可点击区域中拖放TreeViewItems

时间:2011-04-21 06:59:36

标签: c# .net wpf treeview drag-and-drop

关于在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);
    }
}

所以我想做的非常简单,只需在另一个树视图项上拖放一个树视图项时弹出一个消息框。

当我把它放在另一个项目上时,它可以正常工作,如下所示:

enter image description here

但是,如果我稍微拖离项目的边界,它就不起作用,如下所示:

enter image description here

鼠标光标显示为“禁止标志”,抱歉我无法在屏幕截图中看到它。

所以现在我的问题是:如何使第二个条件有效?当丢弃的位置稍微偏离项目的边界时,如何使Drop事件仍然启动?

提前致谢。

2 个答案:

答案 0 :(得分:2)

ControlTemplate的默认TreeViewItem并使Border(包含ContentPresenter的边框)的colspan为2,例如:

<Border
            BorderThickness="{TemplateBinding Border.BorderThickness}"
            Padding="{TemplateBinding Control.Padding}"
            BorderBrush="{TemplateBinding Border.BorderBrush}"
            Background="{TemplateBinding Panel.Background}"
            Name="Bd"
            SnapsToDevicePixels="True"
            Grid.Column="1" Grid.ColumnSpan="2">

请注意,TextBlock旁边的TreeViewItem中的整个区域将变为可点击。您可能想要使用模板来满足您的需求。
enter image description here

下面是使用Dump Control Template Utility提取的TreeViewItem的ControlTemplate。

<?xml version="1.0" encoding="utf-16"?>
<ControlTemplate
    TargetType="TreeViewItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition
                Width="Auto"
                MinWidth="19" />
            <ColumnDefinition
                Width="Auto" />
            <ColumnDefinition
                Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition
                Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <ToggleButton
            IsChecked="False"
            ClickMode="Press"
            Name="Expander">
            <ToggleButton.Style>
                <Style
                    TargetType="ToggleButton">
                    <Style.Resources>
                        <ResourceDictionary />
                    </Style.Resources>
                    <Setter
                        Property="UIElement.Focusable">
                        <Setter.Value>
                            <s:Boolean>False</s:Boolean>
                        </Setter.Value>
                    </Setter>
                    <Setter
                        Property="FrameworkElement.Width">
                        <Setter.Value>
                            <s:Double>19</s:Double>
                        </Setter.Value>
                    </Setter>
                    <Setter
                        Property="FrameworkElement.Height">
                        <Setter.Value>
                            <s:Double>13</s:Double>
                        </Setter.Value>
                    </Setter>
                    <Setter
                        Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate
                                TargetType="ToggleButton">
                                <Border
                                    Background="#00FFFFFF"
                                    Width="19"
                                    Height="13">
                                    <Border
                                        BorderThickness="1,1,1,1"
                                        CornerRadius="1,1,1,1"
                                        BorderBrush="#FF7898B5"
                                        Width="9"
                                        Height="9"
                                        SnapsToDevicePixels="True">
                                        <Border.Background>
                                            <LinearGradientBrush
                                                StartPoint="0,0"
                                                EndPoint="1,1">
                                                <LinearGradientBrush.GradientStops>
                                                    <GradientStop
                                                        Color="#FFFFFFFF"
                                                        Offset="0.2" />
                                                    <GradientStop
                                                        Color="#FFC0B7A6"
                                                        Offset="1" />
                                                </LinearGradientBrush.GradientStops>
                                            </LinearGradientBrush>
                                        </Border.Background>
                                        <Path
                                            Data="M0,2L0,3 2,3 2,5 3,5 3,3 5,3 5,2 3,2 3,0 2,0 2,2z"
                                            Fill="#FF000000"
                                            Name="ExpandPath"
                                            Margin="1,1,1,1" />
                                    </Border>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger
                                        Property="ToggleButton.IsChecked">
                                        <Setter
                                            Property="Path.Data"
                                            TargetName="ExpandPath">
                                            <Setter.Value>
                                                <StreamGeometry>M0,2L0,3 5,3 5,2z</StreamGeometry>
                                            </Setter.Value>
                                        </Setter>
                                        <Trigger.Value>
                                            <s:Boolean>True</s:Boolean>
                                        </Trigger.Value>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>
        <Border
            BorderThickness="{TemplateBinding Border.BorderThickness}"
            Padding="{TemplateBinding Control.Padding}"
            BorderBrush="{TemplateBinding Border.BorderBrush}"
            Background="{TemplateBinding Panel.Background}"
            Name="Bd"
            SnapsToDevicePixels="True"
            Grid.Column="1">
            <ContentPresenter
                Content="{TemplateBinding HeaderedContentControl.Header}"
                ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
                ContentSource="Header"
                Name="PART_Header"
                HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
        </Border>
        <ItemsPresenter
            Name="ItemsHost"
            Grid.Column="1"
            Grid.Row="1"
            Grid.ColumnSpan="2" />
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger
            Property="TreeViewItem.IsExpanded">
            <Setter
                Property="UIElement.Visibility"
                TargetName="ItemsHost">
                <Setter.Value>
                    <x:Static
                        Member="Visibility.Collapsed" />
                </Setter.Value>
            </Setter>
            <Trigger.Value>
                <s:Boolean>False</s:Boolean>
            </Trigger.Value>
        </Trigger>
        <Trigger
            Property="ItemsControl.HasItems">
            <Setter
                Property="UIElement.Visibility"
                TargetName="Expander">
                <Setter.Value>
                    <x:Static
                        Member="Visibility.Hidden" />
                </Setter.Value>
            </Setter>
            <Trigger.Value>
                <s:Boolean>False</s:Boolean>
            </Trigger.Value>
        </Trigger>
        <Trigger
            Property="TreeViewItem.IsSelected">
            <Setter
                Property="Panel.Background"
                TargetName="Bd">
                <Setter.Value>
                    <DynamicResource
                        ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
                </Setter.Value>
            </Setter>
            <Setter
                Property="TextElement.Foreground">
                <Setter.Value>
                    <DynamicResource
                        ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
                </Setter.Value>
            </Setter>
            <Trigger.Value>
                <s:Boolean>True</s:Boolean>
            </Trigger.Value>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition
                    Property="TreeViewItem.IsSelected">
                    <Condition.Value>
                        <s:Boolean>True</s:Boolean>
                    </Condition.Value>
                </Condition>
                <Condition
                    Property="Selector.IsSelectionActive">
                    <Condition.Value>
                        <s:Boolean>False</s:Boolean>
                    </Condition.Value>
                </Condition>
            </MultiTrigger.Conditions>
            <Setter
                Property="Panel.Background"
                TargetName="Bd">
                <Setter.Value>
                    <DynamicResource
                        ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
                </Setter.Value>
            </Setter>
            <Setter
                Property="TextElement.Foreground">
                <Setter.Value>
                    <DynamicResource
                        ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
                </Setter.Value>
            </Setter>
        </MultiTrigger>
        <Trigger
            Property="UIElement.IsEnabled">
            <Setter
                Property="TextElement.Foreground">
                <Setter.Value>
                    <DynamicResource
                        ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                </Setter.Value>
            </Setter>
            <Trigger.Value>
                <s:Boolean>False</s:Boolean>
            </Trigger.Value>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

答案 1 :(得分:0)

为TreeView本身使用AllowDrop属性和Drop事件。

这是来自msdn:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4b8bafe5-ae3e-439d-953a-f534a60dbb2d/

相关问题