Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
How To: Send focus directly to a CheckBox in a TreeViewItem
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 35 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Josh Smith  
View profile  
 More options Jul 30 2008, 2:41 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 14:41:42 -0400
Local: Wed, Jul 30 2008 2:41 pm
Subject: How To: Send focus directly to a CheckBox in a TreeViewItem

Hey all,

I have a WPF programming question for you all.  I just can't find a way to
do this, even after trying several hacks.  Suppose I have a TreeView where
items should all contain CheckBoxs.  I want it so that the user can easily
navigate through the tree and press Spacebar to check/uncheck an item's
CheckBox.  The problem is with input focus.  I find that the TreeViewItems
want to take focus first, requiring me to press an arrow key twice to bring
focus to the contained CheckBox.  I tried setting Focusable on the
ItemContainerStyle to false, but then the keyboard navigation does not work
properly.  I also tried writing some code that handles GotFocus and
GotKeyboardFocus of all TreeViewItems and then use FindName to get at the
CheckBox and give it focus.  This doesn't work either, because focus never
leaves the first CheckBox.

Here's the XAML I am using:
<TreeView ItemsSource="{Binding}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
      <CheckBox
        x:Name="chk"
        Content="{Binding Name}"
        IsChecked="{Binding IsChecked}"
        />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

Anyone ever figured this out before?  If so, what's the trick? This seems
like it should be a simple XAML-only solution, but I can't figure it out!
:(

Thanks,
Josh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pavan Podila  
View profile  
 More options Jul 30 2008, 3:43 pm
From: "Pavan Podila" <pavan.pod...@gmail.com>
Date: Wed, 30 Jul 2008 15:43:35 -0400
Local: Wed, Jul 30 2008 3:43 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Focus management with TreeView and List based controls is pretty tricky and
one of the big pain points in WPF. I still don't have a good hold on this
topic. I had done something similar earlier just that my template for the
TreeViewItem was more complex and had more controls. However due to lack of
time, we decided to provide only mouse support (Sucks .. but that was how it
went).

--
the approach, rather than the solution
...o0O0o...
http://blog.pixelingene.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 3:46 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 15:46:21 -0400
Local: Wed, Jul 30 2008 3:46 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Thanks Pavan.  Of course, that isn't the answer I was hoping for!  :)  Oh
well, it seems I've got my work cut out for me (unless someone can come to
the rescue...).

josh

On Wed, Jul 30, 2008 at 3:43 PM, Pavan Podila <pavan.pod...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 3:47 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 15:47:15 -0400
Local: Wed, Jul 30 2008 3:47 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Hey JG!  This thread is another one to add to your "what we should simplify"
list... ;)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pavan Podila  
View profile  
 More options Jul 30 2008, 4:01 pm
From: "Pavan Podila" <pavan.pod...@gmail.com>
Date: Wed, 30 Jul 2008 16:01:09 -0400
Local: Wed, Jul 30 2008 4:01 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

BTW, did you try calling MoveFocus() on the element that has focus with a
TraversalRequest of Down/Up. Keyboard.FocusedElement should get you the
current element. This is a pretty manual way and not Xaml based (although if
you can get this working you could make it into an attached property).

--
the approach, rather than the solution
...o0O0o...
http://blog.pixelingene.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 4:03 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 16:03:43 -0400
Local: Wed, Jul 30 2008 4:03 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Yes I tried that, but that didn't work either.  It is amazing how difficult
this is.  It should be dead simple, in my opinion.

On Wed, Jul 30, 2008 at 4:01 PM, Pavan Podila <pavan.pod...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pavan Podila  
View profile  
 More options Jul 30 2008, 4:04 pm
From: "Pavan Podila" <pavan.pod...@gmail.com>
Date: Wed, 30 Jul 2008 16:04:41 -0400
Local: Wed, Jul 30 2008 4:04 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Let me know if you figure it out ... I'd love to hear !

--
the approach, rather than the solution
...o0O0o...
http://blog.pixelingene.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 4:05 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 16:05:25 -0400
Local: Wed, Jul 30 2008 4:05 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Hey, you stole my role in this thread!!  :D

On Wed, Jul 30, 2008 at 4:04 PM, Pavan Podila <pavan.pod...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pavan Podila  
View profile  
 More options Jul 30 2008, 4:07 pm
From: "Pavan Podila" <pavan.pod...@gmail.com>
Date: Wed, 30 Jul 2008 16:07:23 -0400
Local: Wed, Jul 30 2008 4:07 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Sorry ...all yours :)) I am just equally curious as I know the amount of
time I wasted (researched)

--
the approach, rather than the solution
...o0O0o...
http://blog.pixelingene.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 4:07 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 16:07:57 -0400
Local: Wed, Jul 30 2008 4:07 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

It's all good, Pavan.  Thanks for your help!!

On Wed, Jul 30, 2008 at 4:07 PM, Pavan Podila <pavan.pod...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 6:25 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 18:25:59 -0400
Local: Wed, Jul 30 2008 6:25 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

I found a solution, but it sucks.  I make the CheckBox Focusable=False, hook
up a custom command on my TreeView that is triggered via keygestures of
Space and Enter, and the execution logic of that command gets the
SelectedItem viewmodel object from the tree and toggles it's IsChecked
property.  YUCK!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pavan Podila  
View profile  
 More options Jul 30 2008, 8:25 pm
From: "Pavan Podila" <pavan.pod...@gmail.com>
Date: Wed, 30 Jul 2008 20:25:15 -0400
Local: Wed, Jul 30 2008 8:25 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Can it get any more convoluted ?! What if I throw in a few more controls
into my TreeViewItem ... that should probably cause annihilation.

--
the approach, rather than the solution
...o0O0o...
http://blog.pixelingene.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr. WPF  
View profile  
 More options Jul 30 2008, 8:29 pm
From: "Dr. WPF" <a...@drwpf.com>
Date: Wed, 30 Jul 2008 19:29:40 -0500
Local: Wed, Jul 30 2008 8:29 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

   Sorry I'm so late to the party, Josh.

   I faced the same issue a while back and actually implemented a  
solution in which I used a static class called VirtualToggleButton to  
expose attached properties that would turn any input element (a  
TreeViewItem, for example) into a toggle button.  In your case, the  
markup would look like this:

   <TreeView ItemsSource="{Binding}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
      <CheckBox Focusable="False"
        x:Name="chk"
        Content="{Binding Name}"
        IsChecked="{Binding IsChecked}"
        />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
  <TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="dw:VirtualToggleButton.IsVirtualToggleButton"  
Value="True" />
      <Setter Property="dw:VirtualToggleButton.IsChecked"  
Value="{Binding IsChecked}" />
    </Style>
  </TreeView.ItemContainerStyle>
</TreeView>

   I guess the /en vogue/ term for this type of thing is /"attached  
behavior".  /(Thank you Mr. Gossman... you've saved me from the overly  
wordy description about leveraging an attached property to extend a  
class in order to add support for yada yada yada...)

   When the IsVirtualToggleButton property is attached to the  
TreeViewItem and set to true, the class monitors the TreeViewItem for  
input events (mouse clicks or key presses) and responds to those  
events just like a toggle button.  A spacebar press or mouse click  
will update the attached IsChecked property.  It even supports the  
IsThreeState property and raises all the expected toggle button events  
(Checked, Unchecked, and Indeterminate) on the target element.   
(Although, it doesn't include the ButtonBase events or properties...  
I'm too lazy for that.)

   You will still need to set Focusable to false on the CheckBox.  Any  
solution will likely require that because you'll never want the  
additional navigation stop for the CheckBox.

   I'm happy to send you the code if you'd like.  Just let me know and  
I'll dig it up.  :-)

   Quoting "wpf-disciples@googlegroups.com" <wpf-disciples@googlegroups.com>:

   Dr. WPF - Online Office at http://www.drwpf.com/blog/[6]

Links:
------
[1] http://blog.pixelingene.com/
[2] http://blog.pixelingene.com/
[3] http://blog.pixelingene.com/
[4] http://blog.pixelingene.com/
[5] http://groups.google.com/group/wpf-disciples?hl=en
[6] http://www.drwpf.com/blog/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 8:37 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 20:37:12 -0400
Local: Wed, Jul 30 2008 8:37 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

That's a very awesome idea!  If you can find the code, I'd love to see it.
Thanks Doc!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
JohnGossman  
View profile  
 More options Jul 30 2008, 8:39 pm
From: JohnGossman <gossmans...@gmail.com>
Date: Wed, 30 Jul 2008 17:39:41 -0700 (PDT)
Local: Wed, Jul 30 2008 8:39 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem
I'm there for you...

Seriously though, that does seem to me to be the value of patterns:
you can tell somebody what the code does using fewer words.

On Jul 30, 5:29 pm, "Dr. WPF" <a...@drwpf.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 10:25 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 22:25:06 -0400
Local: Wed, Jul 30 2008 10:25 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Hey John,

Any chance this difficulty might be ironed out in a subsequent release of
platform?  It's hard to believe that creating a TreeView with checkboxes in
the items, which is easy to navigate via the keyboard and mouse, requires so
much custom work.  This should be dead simple, in my opinion.  If it takes a
Dr. WPF to figure it out, it's way too difficult!  :)

Josh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Brown  
View profile  
 More options Jul 30 2008, 10:49 pm
From: "Mike Brown" <mbrow...@gmail.com>
Date: Wed, 30 Jul 2008 22:49:07 -0400
Local: Wed, Jul 30 2008 10:49 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 10:57 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 22:57:24 -0400
Local: Wed, Jul 30 2008 10:57 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Just in case anyone of is wondering how I finally implemented this hacky,
ugly solution, I've attached the project.  Remove the .DOC extension, and
then unzip it.  What a hack!  Please tell me there's a much simpler, more
elegant way!!

Josh

  TreeViewWithCheckBoxes.zip.DOC
14K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 30 2008, 11:06 pm
From: "Josh Smith" <flappleja...@gmail.com>
Date: Wed, 30 Jul 2008 23:06:26 -0400
Local: Wed, Jul 30 2008 11:06 pm
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

I should point out, Dr. WPF's solution is definitely more *elegant*, but an
order of magnitude more complicated.  :)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr. WPF  
View profile  
 More options Jul 31 2008, 1:42 am
From: "Dr. WPF" <a...@drwpf.com>
Date: Thu, 31 Jul 2008 00:42:57 -0500
Local: Thurs, Jul 31 2008 1:42 am
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

   I agree that it would be nice if we could get this behavior by  
simply styling the existing TreeView/TreeViewItem, but unfortunately,  
without a multiselect TreeView, that’s not possible.  (hint hint)

   Just to follow up for the rest of the group, I've attached a  
version of Josh’s sample that uses the VirtualToggleButton class that  
I described earlier.  After adding my existing class to the project,  
it really was just a matter of pulling out the command bindings and  
handlers and adding two lines of markup to the container style:

     <Setter Property="dw:VirtualToggleButton.IsVirtualToggleButton"  
Value="True" />

     <Setter Property="dw:VirtualToggleButton.IsChecked"  
Value="{Binding IsChecked}" />

   (Okay, I might have mucked with the default dataset, but that was  
just for fun.  J)

   There are a few reasons why I like this approach:

   ·         It’s a write once, use anywhere solution.

   ·         It provides a pure markup solution that is easily  
understood by our designers.

   ·         It has all the sexiness of attached behaviors.

   /(Btw, if you don’t think attached behaviors are sexy, then clearly  
you’re attaching the wrong behaviors!)/

   Quoting "wpf-disciples@googlegroups.com" <wpf-disciples@googlegroups.com>:

   Dr. WPF - Online Office at http://www.drwpf.com/blog/[2]

Links:
------
[1] http://groups.google.com/group/wpf-disciples?hl=en
[2] http://www.drwpf.com/blog/

  TreeViewWithCheckBoxes.dw.zip.DOC
16K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marlon Grech  
View profile  
 More options Jul 31 2008, 3:34 am
From: "Marlon Grech" <marlongr...@gmail.com>
Date: Thu, 31 Jul 2008 09:34:38 +0200
Local: Thurs, Jul 31 2008 3:34 am
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Nice one DOC!!!!! You're the MAN!!!!!! Keep on rocking!

--
Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Other Blog - http://dotnetalgos.wordpress.com/

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 31 2008, 7:49 am
From: "Josh Smith" <flappleja...@gmail.com>
Date: Thu, 31 Jul 2008 07:49:19 -0400
Local: Thurs, Jul 31 2008 7:49 am
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

OK, I'm sold.  Your solution is both more elegant and simpler, considering
that it's "write once, use anywhere."  Hey Doc, do you mind if I blog about
this, or perhaps write a CodeProject article that has both of us as the
authors?

On a side note, you might have noticed that I specifically set the theme of
this Window to Royale.NormalColor.  The reason I did that is because there
is a bug in the CheckBox theme for Aero and Classic!!  When a CheckBox goes
from 'indeterminate' state to 'checked' state in the Aero theme, you need to
mouse over the control for the indeterminate state visual to go away.  Also,
when Windows is using the Classic theme, the Foreground of the CheckBox is
used for the checkmark, which means that you cannot see it when it is
selected (and using a light foreground for selection).  That really sucks!!
If I can muster the enthusiasm, I'll log it on Connect...

Josh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 31 2008, 8:17 am
From: "Josh Smith" <flappleja...@gmail.com>
Date: Thu, 31 Jul 2008 08:17:42 -0400
Local: Thurs, Jul 31 2008 8:17 am
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

Hey Doc,  I've been reviewing your solution more closely.  This is
AWESOME!!  We, I, or you need to publish this, if not only to show people
how powerful attached properties and routed events can be!  Very nice...

Josh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Smith  
View profile  
 More options Jul 31 2008, 9:00 am
From: "Josh Smith" <flappleja...@gmail.com>
Date: Thu, 31 Jul 2008 09:00:34 -0400
Local: Thurs, Jul 31 2008 9:00 am
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem

I've fixed up the implementation a bit, so the user can click on the text in
the treeviewitem and it won't modify the checkstate.  I also optimized the
checkstate verification logic (FooViewModel's IsChecked #region).  The
updated copy, which uses the Doc's VirtualToggleButton coolness, is
attached.  Just waiting for the good Doctor to give me a thumbs-up to write
about this stuff.....  ;)

Josh

  TreeViewWithCheckBoxes[JAS].zip.DOC
16K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pavan Podila  
View profile  
 More options Jul 31 2008, 9:29 am
From: Pavan Podila <pavan.pod...@gmail.com>
Date: Thu, 31 Jul 2008 06:29:54 -0700 (PDT)
Local: Thurs, Jul 31 2008 9:29 am
Subject: Re: How To: Send focus directly to a CheckBox in a TreeViewItem
Doc, thats a fine use of Attached Properties! I was trying to abstract
this a little more for my own needs since I have a TreeViewItem that
has multiple controls, which may need focus.

In your implementation, you are setting Focusable=false and then using
Bindings to show the checked state. In other words we are implementing
a state-machine for each TreeViewItem. If I had to extrapolate for a
TreeViewItem that has multiple controls like a CheckBox + TextBox, how
would you suggest I go about. Right now I am trying to add more states
to my simple StateMachine for each TreeViewItem and forcing a focus()
on the TextBox ...

Would love to hear your views.

Once again, Great job!!

On Jul 31, 9:00 am, "Josh Smith" <flappleja...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 35   Newer >
« Back to Discussions « Newer topic     Older topic »