0

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...

4

1 回答 1

2

不知道它是否仍然重要(最后一个答案是在 2010 年 3 月 25 日),但是对于仍然想知道如何实现这一点的人,我是这样做的:

在样式部分:

    <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>

在列表框中,我使用 ItemContainerStyle 属性,如下所示:

ItemContainerStyle="{StaticResource myLBStyle}

我花了一段时间才找到,但它就在这里。希望有人可以使用!

也很方便:

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

此致,

山姆

于 2011-02-13T11:01:31.337 回答