Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

WPF ListBox Selecteditem colors

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Damian - 13 Feb 2007 08:51 GMT
I'm struggling with knowing how to change the colors used when selecting an
item in a databound ListBox. If I have a listbox which uses a datatemplate
for items, I  don't know how to use triggers to change the foreground and
background color in the template (if it was a ListBoxItem, I would say
ListBoxitem.IsSelected, but it is a Person for example) based on the item
being selected.

The problem is that the default selection foreground and background color
don't work with my DataTemplate's Gradient colors...text gets hidden
RobinS - 13 Feb 2007 18:20 GMT
> I'm struggling with knowing how to change the colors used when selecting
> an
[quoted text clipped - 7 lines]
> The problem is that the default selection foreground and background color
> don't work with my DataTemplate's Gradient colors...text gets hidden

You might try posting this to
microsoft.public.windows.developer.winfx.avalon, and/or the WPF group on
the MSDN Forums.

Robin S.
Walter Wang [MSFT] - 14 Feb 2007 06:21 GMT
Hi,

Sorry for delayed reply.

This question is actually not easy as it looks like. As a workaround, we
could bind the Selected item to a different DataTemplate, Josh Smith has a
blog on this technique:

#Josh Smith on WPF : Specializing the Selected Item's DataTemplate
http://www.infusionblogs.com/blogs/jsmith/archive/2006/08/09/699.aspx

By the way, currently there's no dedicated managed newsgroups for WPF yet,
so posting here is ok for now.

Sincerely,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Damian - 14 Feb 2007 13:13 GMT
Thanks Walter, this was exactly what I needed. The style using a
relativesource binding is something I couldn't really get a grasp on, this is
perfect (though not immediately intuitive).

For others who are intersted here is the datatemplate that solves this issue:
<DataTemplate x:Key="itemTemplate">
     <Grid>
       <Grid.Resources>
         <!-- This style is applied to the StackPanel which contains the
controls only
              displayed by the ListBox's selected item. -->
         <Style x:Key="selectedPanelStyle">
           <Style.Triggers>
             <DataTrigger
               Binding="{Binding
                         RelativeSource=
                         {
                            RelativeSource
                            Mode=FindAncestor,
                            AncestorType={x:Type ListBoxItem}
                         },
                         Path=IsSelected
                        }"
               Value="False">
               <Setter Property="StackPanel.Visibility" Value="Collapsed" />
             </DataTrigger>
           </Style.Triggers>
         </Style>
       </Grid.Resources>
       
       <Grid.RowDefinitions>
         <RowDefinition />
         <RowDefinition />
       </Grid.RowDefinitions>
       
       <!-- This is displayed whether the ListBoxItem is selected or not. -->
       <TextBlock Text="{Binding Path=Name}"/>
       
       <!-- This is only displayed when the ListBoxItem is selected. -->
       <StackPanel Grid.Row="1" Style="{StaticResource selectedPanelStyle}">
         <Button>Edit Item...</Button>
       </StackPanel>
     </Grid>
   </DataTemplate>

Note the: Binding="{Binding
                         RelativeSource=
                         {
                            RelativeSource
                            Mode=FindAncestor,
                            AncestorType={x:Type ListBoxItem}
                         },
                         Path=IsSelected
                        }"

That is what allows you to know whether or not the item is selected.

> Hi,
>
[quoted text clipped - 35 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.