The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
ListBoxItem change Back- & Foreground when selected RRS feed

  • Question

  • How can one achieve that?
    There are no control template triggers.
    MouseLeftButtonDown event handler doesn't help at all.
    I have tried something like this?
            private void lstBxMyAudits_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
            { 
                ListBox listBox = (ListBox)sender; 
                ListBoxItem listBoxItem = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem((Auditplan)listBox.SelectedItem); 
                listBox.Background = new SolidColorBrush(Color.FromArgb(100, 45, 23, 45)); 
            } 
     
    but without effect.

    And by default the Foreground for ListBoxItems is white, and when clicked is blue.
    So, I have defined a style for them like follow
                            <Style TargetType="TextBlock" x:Key="AuditplanStyle"
                                <Setter Property="FontSize" Value="22" /> 
                                <Setter Property="Foreground" Value="Black" /> 
                            </Style> 
     

    So now the Foreground is black, even when clicked.
    How can I achieve changing the Fore and Background of ListBoxItems when they are clicked?

    Thanks and regards.
    Friday, January 21, 2011 2:54 PM

Answers

All replies

  • I have listed steps to do that using Blend sometime back. Now I am not able to find it out.

    Actually you have to set the VisualStates.SelectionStates (Unselected & Selected states) template of the ListBoxItem. You can open the page in Blend and right click on a ListBox's Item and selecte Edit Template -> Edit Copy. Then you have to go to States tab and in Visual States you have edit that above states.

    The final one will be as in a post in this thread: http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/f6c94f4c-2f2e-4286-bbdd-148b7a89416d#9fd435cc-f8d0-4886-98c5-415520216142.
    Friday, January 21, 2011 4:44 PM

  • Actually you have to set the VisualStates.SelectionStates (Unselected & Selected states) template of the ListBoxItem.
    So VisualStateManager, VisualStates.SelectionStates, etc. = WPFs Style&ControlTemplate style/event triggers???
    Monday, January 24, 2011 12:12 PM
  • So VisualStateManager, VisualStates.SelectionStates, etc. = WPFs Style&ControlTemplate style/event triggers???
    Yes :) In Silverlight you use VisualStates where you would use ControlTemplate Triggers in WPF.
    Monday, January 24, 2011 2:34 PM
  • Yes :) In Silverlight you use VisualStates where you would use ControlTemplate Triggers in WPF.
    Why? Not if it had any importance. Just curious.
    To me using triggers is easier and more intuitive.
    I guess because ControlTemplate triggers implementation doesn't fit into silverlight's framework.
    Monday, January 24, 2011 3:03 PM
  • I'm not sure why they changed things, although I actually find visual states a lot easier to work with. There are a lot of things in both silverlight and WPF that I miss when switching between them (CommandManager, VisualStates...)
    Monday, January 24, 2011 3:58 PM