Click here to Skip to main content
15,888,984 members
Home / Discussions / WPF
   

WPF

 
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 
AnswerRe: ZoomIn and zoomout Pin
Mark Salsbery6-Jan-10 8:49
Mark Salsbery6-Jan-10 8:49 
AnswerRe: ZoomIn and zoomout Pin
April Fans6-Jan-10 19:14
April Fans6-Jan-10 19:14 
QuestionHide controlbox or customize controlbox of WPF window [modified] Pin
Member 26504535-Jan-10 18:34
Member 26504535-Jan-10 18:34 
QuestionDatabinding and Value Converter questions Pin
Michael Eber4-Jan-10 10:39
Michael Eber4-Jan-10 10:39 
AnswerRe: Databinding and Value Converter questions Pin
Abhinav S4-Jan-10 16:05
Abhinav S4-Jan-10 16:05 
GeneralRe: Databinding and Value Converter questions Pin
Michael Eber5-Jan-10 6:27
Michael Eber5-Jan-10 6:27 
GeneralRe: Databinding and Value Converter questions Pin
Abhinav S5-Jan-10 17:00
Abhinav S5-Jan-10 17:00 
QuestionGridSplitter Behaviour Pin
rj_hogan4-Jan-10 5:35
rj_hogan4-Jan-10 5:35 
AnswerRe: GridSplitter Behaviour Pin
Pete O'Hanlon4-Jan-10 9:25
mvePete O'Hanlon4-Jan-10 9:25 
GeneralRe: GridSplitter Behaviour Pin
rj_hogan5-Jan-10 0:46
rj_hogan5-Jan-10 0:46 
GeneralRe: GridSplitter Behaviour Pin
Pete O'Hanlon5-Jan-10 2:16
mvePete O'Hanlon5-Jan-10 2:16 
QuestionWWF Pin
Ramkumar_S3-Jan-10 14:36
Ramkumar_S3-Jan-10 14:36 
AnswerRe: WWF Pin
ProtoBytes4-Jan-10 3:52
ProtoBytes4-Jan-10 3:52 
QuestionHow to animate To/From an Auto value? Pin
spiritboy3-Jan-10 8:04
spiritboy3-Jan-10 8:04 
AnswerRe: How to animate To/From an Auto value? Pin
Abhinav S3-Jan-10 16:39
Abhinav S3-Jan-10 16:39 
Questioncreating GridViewColumn.CellTemplate via C# ? Pin
Mohammad Dayyan2-Jan-10 23:43
Mohammad Dayyan2-Jan-10 23:43 

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.