我正在尝试创建某种窗口或工具箱,它可以被其标题部分拖动,就像任何窗口一样。出于某种原因,它似乎对我不起作用。标题移动但身体部位保持不动。
这是我的代码:
BehaviorCollection behaviorCollection = Interaction.GetBehaviors(this._explorerHeader);
MouseDragElementBehavior mouseDragBehavior = new MouseDragElementBehavior { ConstrainToParentBounds = false };
mouseDragBehavior.Dragging += (s1, e1) =>
{
Canvas.SetLeft(this._explorerBody, (Double)_explorerHeader.GetValue(Canvas.LeftProperty));
Canvas.SetTop(this._explorerBody, ((Double)_explorerHeader.GetValue(Canvas.TopProperty) + _explorerHeader.ActualHeight));
};
behaviorCollection.Add(mouseDragBehavior);
更新
这是控件的控件模板:
<Style x:Key="ApplicationExplorerDefaultStyle" TargetType="Controls:ApplicationExplorer">
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="300" />
<Setter Property="CornerRadius" Value="0" />
<Setter Property="Background" Value="{StaticResource LightBlue}" />
<Setter Property="HeaderBackgroundBrush" Value="{StaticResource VeryLightBlue}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="TreeItemTemplate" Value="{StaticResource ChildTemplate}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Controls:ApplicationExplorer">
<Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderBrush}"
BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}"
CornerRadius="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CornerRadius}"
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"
Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height}" >
<Grid x:Name="Root" MinWidth="100">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="ExplorerHeader" Grid.Row="0" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderBackgroundBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{TemplateBinding Title}" Style="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TitleStyle}" VerticalAlignment="Center" />
<Controls:LayoutToggleButton Grid.Column="2" x:Name="LayoutButton" Cursor="Hand" />
</Grid>
<Border x:Name="ExplorerBody" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"
Grid.Row="1" HorizontalAlignment="Stretch" BorderThickness="0" >
<toolkit:TreeViewDragDropTarget AllowedSourceEffects="Copy" x:Name="treeViewDropTarget"
HorizontalAlignment="Left" VerticalAlignment="Top" >
<sdk:TreeView x:Name="treeView" ItemsSource="{TemplateBinding Nodes}" Background="Transparent" BorderThickness="0"
ItemTemplate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TreeItemTemplate}"
HorizontalAlignment="Left" Margin="10 0 0 0" />
</toolkit:TreeViewDragDropTarget>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
有什么建议吗?