Saturday, 15 June 2013

c# - In a HierarchicalDataTemplate Label transforms to TextBlock -


While trying to drag and drop in my application, I had to face some strange behavior.

XAML:

  & lt; i like a  HierarchicalDataTemplate  in a  to represent my data structure like TreeView  I am using; TreeView Name = "treeViewNotes" AllowDrop = "True" PreviewMouseLeftButtonDown = "PreviewViewNotes_PreviewMouseLeftButtonDown" PreviewMonMove = "treeViewNotes_PreviewMouseMove" drop = "TreeView Notes" dragentor = "TreeView_DressorEnter" selectedElectamChanged = "TreeViewsActionedItemChanged" & gt; & Lt; TreeView.Resources & gt; & Lt; Hierarchical dataTypeplate dataType = "{x: type local: notecountry}" item source = "{binding notes}" & gt; & Lt; StackPanel Orientation = "Horizontal" & gt; & Lt; Image height = "16" source = "{Binding TreeViewIcon}" tag = "{binding self}" /> & Lt; Label content = "{binding title}" tag = "{binding self}" margin = "3" /> & Lt; / StackPanel & gt; & Lt; / HierarchicalDataTemplate & gt; & Lt; Hierarchical dataTypeplate dataType = "{x: type local: textnote}" & gt; & Lt; StackPanel Orientation = "Horizontal" & gt; & Lt; Image height = "16" source = "{Binding TreeViewIcon}" tag = "{binding self}" /> & Lt; Label content = "{binding title}" tag = "{binding self}" margin = "3" /> & Lt; / StackPanel & gt; & Lt; / HierarchicalDataTemplate & gt; & Lt; /TreeView.Resources> & Lt; / TreeView & gt;  

Code:

  Private Zero buttonCreateNote_Click (Object Sender, RoutedEventArgs e) {ObservableCollection & LT; NoteCategory & gt; Note program list = new inspection qualification & lt; Note Cache & gt; (); Note Category Category 1 = New Note Category ("Category 1", Properties, Resources. Echo); Note note 1 note = 1 new text note ("text note 1", property.product note note, category 1, "blah blah. TextNote textNote2 = new text note ("text note 2", property.product note notices, category1, "blah blah.not content for 2"); Note Category Category 2 = New Note Category ("Category 2", Properties, Resources. Echo); TextNote textNote4 = new text note ("text note 4", property.product note notices, category 2, "blah blah.Not content for 4"); TextNote textNote5 = new text note ("text note 5", property.product note notices, category 2, "content for blah blah note 5"); NoteCategoriesList.Add (CATEGORY1); NoteCategoriesList.Add (CATEGORY2); TreeViewNotes.ItemsSource = Note list list; }  

RESULT: Enter image details here

< P> This works fine, so now I want to be able to drag & drop notes between categories, then I applied the code partially to the code

< Pre> Private Zero TreeViewNotes_PreviewMouseMove (Object Sender, MouseInventErgus e) {Point MySpace = E. Getposition (empty); Vector difference = uTill Draggrad Start Point - MousePos; If (Utils.IsDragDropping == False & amp; (e.LeftButton == MouseButtonState.Pressed || e.RightButton == MouseButtonState.Pressed) & amp; Math.Abs ​​(diff.X ) & Gt; SystemParameters.MinimumHorizontalDragDistance || Math.Abs ​​(diff.Y) & gt; SystemParameters.MinimumVerticalDragDistance)) {// dragged ListViewItem TreeView TreeView = Get this as TreeView; If (E. is the original source image; e.OriginalSource is labeled) {framework element elem = E. As OriginSource FrameworkElement; If (elem.Tag == tap) MessageBox.Show (elem.GetType (+) "\ n \ nNULL"); Other messageboxes Showing (elem.GetType () + "\ n \ n" + elem.Tag.ToString ()); } And MessageBox.Show (e.OriginalSource.GetType (.) ToString ()); }}

The entire operation key is composing the tag in the TextNote object, so I can remove it when the user < Leaves code> labels or image to TreeViewItem . And it works for image . But when I try to drag by clicking on the label then the e.realinal source is actually a blank with a blank tag in the form of a TextBlock Handler comes on. I can not fire the label event, even if I do not click any and drag the mouse. The question is why?

I did an alternative solution by using a TextBlock instead of a label HierarchicalDataTemplate and it works, but I Still curious why other views do not work.


No comments:

Post a Comment