概述:最近在做SL的拖动,然后使用了SL toolkit提供的控件,感觉并不是很好,不能实现我需要的功能,比如拖动时的复制增加,TreeView首选项不能拖动等,还有不能在 childwindows里面使用这种拖动控件,所以自己写了一个TreeView 拖动的代码。
最近在做SL的拖动,然后使用了SL toolkit提供的控件,感觉并不是很好,不能实现我需要的功能,比如拖动时的复制增加,TreeView首选项不能拖动等,还有不能在childwindows里面使用这种拖动控件,所以自己写了一个TreeView 拖动的代码。
有一个效果截图给大家看下:
下面是XAML方面的代码:当然如果右边的控件不为listbox的话,换成别的控件也是可以的。
<Canvas x:Name="LayoutRoot" AllowDrop="True"
MouseLeftButtonUp="LayoutRoot_MouseLeftButtonUp"
MouseMove= "LayoutRoot_MouseMove">
<!--这个是左边的ListBox,鼠标将从此ListBox拖出图标-->
<ListBox Name="listBox1" Background="AliceBlue" Width="100"
HorizontalAlignment= "Left" VerticalAlignment="Top"
Height= "550" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Border BorderThickness="1">
<TextBlock Text="{Binding IcoConcent}"
Height= "15" Margin="0,5,6,0"
Tag= "{Binding IcoName}"
MouseLeftButtonDown= "Image_MouseLeftButtonDown"
HorizontalAlignment= "Center"></TextBlock>
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--这个在鼠标拖动过程中显示的图标-->
<TextBlock Name="tab" Opacity="0.5" Width="30" Height="30"
Margin= "0,5,6,0" Visibility="Collapsed" HorizontalAlignment="Center" />
<sdk:TreeView Name="treDetail" Margin="200 0 0 0"
Background="AliceBlue" Height="550" Width="300" >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="ContextMenu1">
<toolkit:MenuItem Header="{StaticResource Delete}" Name="treiDelete" x:FieldModifier="public"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</sdk:TreeView>
下面是后台代码: 这边最重要的就是VisualTreeHelper.FindElementsInHostCoordinates类 他获得了鼠标移动到treeview 上需要的treeviewitem。
' 标示是否按下鼠标左键
Private leftMouseflag As Boolean = False
' 右边ListBox的结果集合
Private Sub Image_MouseLeftButtonDown
(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
'鼠标在ListBox中按下准备拖出图片的时候,设置leftMouseflag为true,并且设置Image为可见
leftMouseflag = True
Dim _textbox As TextBlock = TryCast(sender, TextBlock)
Dim _point As Point = e.GetPosition(_textbox)
Me.tab.Text = _textbox.Text
Me.tab.Tag = _textbox.Tag
Me.tab.SetValue(Canvas.LeftProperty, _point.X)
Me.tab.SetValue(Canvas.TopProperty, _point.Y - 5)
Me.tab.Visibility = Visibility.Visible
End Sub
Private Sub LayoutRoot_MouseLeftButtonUp
(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
'如果得知鼠标左键松动的位置是右边的ListBox上时则为右边的ListBox添加一列
Dim point As Point = e.GetPosition(LayoutRoot)
If point.X > 200 AndAlso point.X < 500 AndAlso point.Y
< 650 AndAlso leftMouseflag = True Then
If listBox1.SelectedItem IsNot Nothing Then
Me.tab.Visibility = Visibility.Collapsed
Dim returnValue As IEnumerable(Of UIElement)
returnValue = VisualTreeHelper.FindElementsInHostCoordinates
(e.GetPosition(Nothing), treDetail)
For Each _r As Object In returnValue
If _r.ToString = "System.Windows.Controls.TreeViewItem" Then
p_ingteger = p_ingteger + 1
Dim _tre As New TreeViewItem
_tre.Header = Me.tab.Text
_tre.IsExpanded = True
_r.Items.Add(_tre)
Exit For
End If
Next
listBox1.SelectedItem = Nothing
leftMouseflag = False
End If
Else
Me.tab.Visibility = Visibility.Collapsed
leftMouseflag = False
End If
End Sub
Private Sub LayoutRoot_MouseMove
(ByVal sender As Object, ByVal e As MouseEventArgs)
'让图片跟随鼠标的移动而移动
Dim _point As Point = e.GetPosition(sender)
Me.tab.SetValue(Canvas.LeftProperty, _point.X)
Me.tab.SetValue(Canvas.TopProperty, _point.Y - 5)
End Sub
关于treeview 内部的拖动 我想可以根据treeview.selecteditem或的鼠标的焦点 然后判断它是否移动出treeviewitem的范围来确定它是否在treeview控件做过拖动行为。
标签:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:网络转载