IsFocused Trigger

1,275 views
Skip to first unread message

Corrado Cavalli

unread,
Jan 21, 2009, 5:29:29 AM1/21/09
to wpf-di...@googlegroups.com
Today's probably not my day... J

I just want to define a trigger that changes a listbox background color when
focused:

<Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}">

<Style.Triggers>

<Trigger
Property="IsFocused" Value="True">


<Setter Property="Background" Value="#FFDFD331"/>

</Trigger>

</Style.Triggers>

<Setter Property="Background"
Value="#FFA2D27F"/>

</Style>

<ListBox x:Name="lb1" HorizontalAlignment="Left"
GotFocus="ListBox_GotFocus" Margin="14,19,0,36" Width="114"
Style="{DynamicResource ListBoxStyle1}" >

</ListBox>

Investigating the code I've seen that, inside Listbox's GotFocus event
IsFocused is still false, doing the same with a Textbox I get True, any idea
about the reason of this difference?

-Corrado

winmail.dat

Corrado Cavalli

unread,
Jan 21, 2009, 5:29:29 AM1/21/09
to wpf-di...@googlegroups.com
winmail.dat

Tamir Khason

unread,
Jan 21, 2009, 6:08:13 AM1/21/09
to wpf-di...@googlegroups.com
It because ListBoxItem is Focused, rather then ListBox itself

--
Tamir http://khason.net/

Eric Burke

unread,
Jan 21, 2009, 6:10:31 AM1/21/09
to wpf-di...@googlegroups.com

Does the ListBox have items? If so, it's might be focused on one of its children.

From the docs for UIElement.GotFocus:

"Because this event uses bubbling routing, the element that receives focus
might be a child element instead of the element where the event handler is
actually attached. Check the Source in the event data to
determine the actual element that gained focus"

Josh Smith

unread,
Jan 21, 2009, 6:46:07 AM1/21/09
to wpf-di...@googlegroups.com
The Trigger's Property should point to IsKeyboardFocusWithin, not IsFocused.

Josh

Corrado Cavalli

unread,
Jan 21, 2009, 6:41:37 AM1/21/09
to wpf-di...@googlegroups.com
That's what I presumed, but so, what's the quickest way to change listbox's
background color using XAML?

-----Original Message-----
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
On Behalf Of Tamir Khason
Sent: mercoledì 21 gennaio 2009 12:08
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: IsFocused Trigger


It because ListBoxItem is Focused, rather then ListBox itself

On Wed, Jan 21, 2009 at 12:29 PM, Corrado Cavalli
<corrado...@gmail.com> wrote:

--
Tamir http://khason.net/

Corrado Cavalli

unread,
Jan 21, 2009, 7:06:03 AM1/21/09
to wpf-di...@googlegroups.com

Absolutely right Josh!

As I’ve said, today’s not my day…

 

-Corrado

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Josh Smith
Sent: mercoledì 21 gennaio 2009 12:46
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: IsFocused Trigger

 

The Trigger's Property should point to IsKeyboardFocusWithin, not IsFocused.

Josh

On Wed, Jan 21, 2009 at 5:29 AM, Corrado Cavalli <corrado...@gmail.com> wrote:

Eric Burke

unread,
Jan 21, 2009, 7:13:23 AM1/21/09
to wpf-di...@googlegroups.com
Does your application have a Menu Bar or Tool Bar (or any custom Focus Scopes)?  If so, if you use the IsKeyboardFocusWithin, you might see some odd behavior when you click a menu item or toolbar button because of the way WPF handles logical vs. keyboard focus.  [e.g., when the user clicks a toolbar button, which receives the keyboard focus, you might see your ListBox flash back to its unfocused color.  I haven't tried this with your example, but I have run into issues with triggers and focus scopes in the past.]

There's no single property that I know of that can tell you whether logical focus is in your tree or not (e.g., IsLogicalFocusWithin).  You probably would have to write a little helper class which has an attached property called IsLogicalFocusWithin which does the work for you.


From: Josh Smith <flappl...@gmail.com>
To: wpf-di...@googlegroups.com
Sent: Wednesday, January 21, 2009 6:46:07 AM

Subject: [WPF Disciples] Re: IsFocused Trigger

Corrado Cavalli

unread,
Jan 21, 2009, 7:25:49 AM1/21/09
to wpf-di...@googlegroups.com

Well,

Mine was indeed a test created for another issue.

What about using an event trigger tied to Focused Event like Silverlight does?

 

                               <Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}">

                                               <Style.Resources>

                                                               <Storyboard x:Key="OnGotFocus1">

                                                                              <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">

                                                                                              <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF4FDA24"/>

                                                                              </ColorAnimationUsingKeyFrames>

                                                               </Storyboard>

                                               </Style.Resources>

                                               <Style.Triggers>

                                                               <EventTrigger RoutedEvent="FocusManager.GotFocus">

                                                                              <BeginStoryboard Storyboard="{StaticResource OnGotFocus1}"/>

                                                               </EventTrigger>

                                               </Style.Triggers>

                               </Style>

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Eric Burke
Sent: mercoledì 21 gennaio 2009 13:13
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: IsFocused Trigger

 

Does your application have a Menu Bar or Tool Bar (or any custom Focus Scopes)?  If so, if you use the IsKeyboardFocusWithin, you might see some odd behavior when you click a menu item or toolbar button because of the way WPF handles logical vs. keyboard focus.  [e.g., when the user clicks a toolbar button, which receives the keyboard focus, you might see your ListBox flash back to its unfocused color.  I haven't tried this with your example, but I have run into issues with triggers and focus scopes in the past.]

There's no single property that I know of that can tell you whether logical focus is in your tree or not (e.g., IsLogicalFocusWithin).  You probably would have to write a little helper class which has an attached property called IsLogicalFocusWithin which does the work for you.

From: Josh Smith <flappl...@gmail.com>
To: wpf-di...@googlegroups.com
Sent: Wednesday, January 21, 2009 6:46:07 AM
Subject: [WPF Disciples] Re: IsFocused Trigger

Reply all
Reply to author
Forward
0 new messages