0
votes

I dynamically create a collection of stackpanels in my listbox. In this stackpanel are contained labels and checkbox horizontally aligned.

The problem is when I click on a stackpanel, the selection is unreadable because the line become dark-blue whereas the letters stay black so black on blue, you see nothing. How can I dynamically change the forecolor of the selected elements in the stackpanel? I say dynamically and not in the xml file, because all those elements are dynamically created from a database.

I have code similar to this:

foreach (var utilis in item.user)
{
    StackPanelWithID ligne = new StackPanelWithID();
    ligne.Orientation = Orientation.Horizontal;
    ligne.ID = utilis.TRIGRAMME;
    ligne.Height = 21;
    Label l = new Label();
    l.Width = 120;
    Label l2 = new Label();
    l2.Width = 145;
    CheckBox cbEntretien = new CheckBox();
}

contentpresenter won't work... I tried several way to position it... So, I found a way to circle the problem ... in the app.xaml :

 <Application.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
                        <Style.Resources>
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
                        </Style.Resources>
    </Style>
</Application.Resources>

Thus the background of selected items is clearer so that the user can still read text of the selected listboxitems.

and every listboxitem is concerned.

and yet... I would love to know how on earth it's possible to change the selected item's text color in list box.. if I manage to get the answer I'll keep you in touch...


I did this...

<ControlTemplate TargetType="ListBoxItem">
                    <ContentPresenter>
                        <ControlTemplate.Triggers>

                            <Trigger Property="IsSelected" Value="true">

                            <Setter Property="Background"
                                        Value="Red"/>

                        </Trigger>

                    </ControlTemplate.Triggers>
                        </ContentPresenter>
                </ControlTemplate>

but stll not working, it says the trigger property can't be found in ControlTemplate... I tried to add it after the trigger property, but not working either...


I tried something like this in the App.xaml : "

<Style x:Key="SimpleListBoxItem" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" <!--can't find the text property so try to act on the Background color to set it to a different color than dark blue-->
                                            Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>"

and in the particular xaml file where my listbox is :

<ListBox Margin="9,64,8,313" Loaded="lstUtilisateurs_Loaded" Name="lstUtilisateurs" ItemContainerStyle="{StaticResource SimpleListBoxItem}"/>

but when executing, nothing appears anymore in the listbox, nothing... I don't get it...

1
Your object is created dynamically, but the style can be set statically. For example, you can use the Control Template in WPF.xandy
When you say you dynamically create stackpanels for the list box, do you mean you create them programmatically?Scott J
Yes, I mean programmatically sorry what I have is something like that : " foreach (var utilis in item.user) { StackPanelWithID ligne = new StackPanelWithID(); ligne.Orientation = Orientation.Horizontal; ligne.ID = utilis.TRIGRAMME; ligne.Height = 21; Label l = new Label(); l.Width = 120; Label l2 = new Label(); l2.Width = 145; CheckBox cbEntretien = new CheckBox();... I'm gonna try templates but it's quite hard for me..Anna
Yeah, WPF template is quite complicated. I suggest you grab a book about WPF. The template engine is actually extremely powerful and fun to play with.xandy

1 Answers

2
votes

Dunno if it still matters (last answer was on Mar 25 2010), but for the people who are still wondering how to accomplish this I did it this way:

In the Style part:

    <Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}">

        <Style.Resources>

            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> <!-- makes the background color transparent, removes backcolor border-->


            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/> <!-- Sets the textcolor of the selected text to red -->

        </Style.Resources>
    </Style>

In the Listbox I use then the ItemContainerStyle property like this:

ItemContainerStyle="{StaticResource myLBStyle}

Took me a while to find, but here it is. Hope that someone can use it!

Also handy:

http://msdn.microsoft.com/en-us/library/ms603164.aspx

Best Regards,

Sam