Click here to Skip to main content
15,913,669 members
Home / Discussions / WPF
   

WPF

 
Question3D Rotation Pin
sajib_bd7-Jan-10 6:04
sajib_bd7-Jan-10 6:04 
QuestionBind to Single Element in Itemssource Using XAML Pin
C.J.7-Jan-10 5:42
C.J.7-Jan-10 5:42 
AnswerRe: Bind to Single Element in Itemssource Using XAML Pin
Abhinav S7-Jan-10 5:59
Abhinav S7-Jan-10 5:59 
GeneralRe: Bind to Single Element in Itemssource Using XAML Pin
C.J.7-Jan-10 6:04
C.J.7-Jan-10 6:04 
GeneralRe: Bind to Single Element in Itemssource Using XAML Pin
Abhinav S7-Jan-10 6:16
Abhinav S7-Jan-10 6:16 
GeneralRe: Bind to Single Element in Itemssource Using XAML Pin
C.J.7-Jan-10 6:47
C.J.7-Jan-10 6:47 
QuestionWPF Datagrid Style Pin
reibor6-Jan-10 10:12
reibor6-Jan-10 10:12 
AnswerRe: WPF Datagrid Style Pin
Mark Salsbery6-Jan-10 12:37
Mark Salsbery6-Jan-10 12:37 
QuestionArgghh my controls are disabled and cannot figure out why!!! Pin
Michael Eber6-Jan-10 9:26
Michael Eber6-Jan-10 9:26 
AnswerRe: Argghh my controls are disabled and cannot figure out why!!! Pin
Ian Shlasko7-Jan-10 3:09
Ian Shlasko7-Jan-10 3:09 
GeneralRe: Argghh my controls are disabled and cannot figure out why!!! Pin
Michael Eber7-Jan-10 7:47
Michael Eber7-Jan-10 7:47 
GeneralRe: Argghh my controls are disabled and cannot figure out why!!! Pin
Ian Shlasko7-Jan-10 10:35
Ian Shlasko7-Jan-10 10:35 
Questionhow to navigate from 1 page to another Pin
hotthoughtguy6-Jan-10 8:18
hotthoughtguy6-Jan-10 8:18 
AnswerRe: how to navigate from 1 page to another Pin
Mark Salsbery6-Jan-10 8:47
Mark Salsbery6-Jan-10 8:47 
AnswerRe: how to navigate from 1 page to another Pin
April Fans6-Jan-10 19:10
April Fans6-Jan-10 19:10 
Questionsubclassing a treeviewitem Pin
hb521342146-Jan-10 4:21
hb521342146-Jan-10 4:21 
AnswerRe: subclassing a treeviewitem Pin
AspDotNetDev6-Jan-10 20:53
protectorAspDotNetDev6-Jan-10 20:53 
GeneralRe: subclassing a treeviewitem Pin
hb521342147-Jan-10 6:26
hb521342147-Jan-10 6:26 
QuestionRe: subclassing a treeviewitem Pin
AspDotNetDev7-Jan-10 13:44
protectorAspDotNetDev7-Jan-10 13:44 
AnswerRe: subclassing a treeviewitem Pin
hb521342147-Jan-10 15:07
hb521342147-Jan-10 15:07 
GeneralRe: subclassing a treeviewitem Pin
hb521342147-Jan-10 15:14
hb521342147-Jan-10 15:14 
GeneralRe: subclassing a treeviewitem Pin
AspDotNetDev7-Jan-10 15:36
protectorAspDotNetDev7-Jan-10 15:36 
GeneralRe: subclassing a treeviewitem Pin
hb521342148-Jan-10 7:15
hb521342148-Jan-10 7:15 
GeneralRe: subclassing a treeviewitem Pin
hb521342148-Jan-10 7:46
hb521342148-Jan-10 7:46 
I think I'll write an article on this, because this TreeView issue really bugs me. I seem to always run it and it drives me nuts. Prior to now, I've just handled event in the TreeView, but its so much nicer for things to connect directly. Alas, I have a technique that works quite nicely.

To solve this problem, create an interface that enumerates command bindings. Say:
interface ICmdHandler {
   IEnumerable<CommandBinding> CommandBindings { get; }
}

Next derive a class from TreeViewItem that determines if the header object is a command handler and then link the commands if it is.
class TVICommandForwarder : TreeViewItem {
   protected override void OnHeaderChanged(object oldHeader, object newHeader) {
      base.OnHeaderChanged(oldHeader, newHeader);
      ICmdHandler oldCH = oldHeader as ICmdHandler;
      ICmdHandler newCH = newHeader as ICmdHandler;
      if (oldCH != null)
         foreach (CommandBinding cb in oldCH.CommandBindings)
            CommandBindings.Remove(cb);
      if (newCH != null)
         foreach (CommandBinding cb in newCH.CommandBindings)
            CommandBindings.Add(cb);
   }
   protected override DependencyObject GetContainerForItemOverride() {
      return new TVICommandForwarder();
   }
}

Notice that if the header changes, it unlinks commands from the old one and links commands from the new header. Also notice that GetContainerForItemOverride() generates a new TVICommandForwarder class, which makes sure that any child nodes also support this mechanism.

Next, one needs to derive a class from TreeView so that it builds nodes using the TVICommandForwarder class.
class TVCommandForwarder : TreeView {
   protected override DependencyObject GetContainerForItemOverride() {
      return new TVICommandForwarder();
   }
}


With that, now any object that gets loaded into the tree automatically gets its command handlers linked if the ICmdHandler interface is implemented. Now that is nice.

If anyone sees a fundamental error in my approach, please let me know. Otherwise, in the next few days I'll write up an article about this technique.
QuestionZoomIn and zoomout Pin
Member 47046755-Jan-10 18:36
Member 47046755-Jan-10 18:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.