由于颜色原因,列表框中选定的项目无法读取

发布于 2024-08-27 03:05:08 字数 2986 浏览 9 评论 0原文

我在列表框中动态创建堆栈面板的集合。在此堆栈面板中包含水平对齐的标签和复选框。

问题是,当我单击堆栈面板时,选择内容不可读,因为线条变为深蓝色,而字母保持黑色,因此蓝色上的黑色,您什么也看不到。 如何动态更改堆栈面板中所选元素的前景色?我说的是动态的,而不是在 xml 文件中,因为所有这些元素都是从数据库动态创建的。

我有与此类似的代码:

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...我尝试了几种方法来定位它... 所以,我找到了一种方法来解决这个问题...... 在 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>

因此所选项目的背景更清晰,以便用户仍然可以阅读所选列表框项目的文本。

并且每个列表框项目都受到关注。

然而...我很想知道到底如何可以更改列表框中所选项目的文本颜色..如果我设法得到答案,我会与您保持联系...


我这样做了...

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

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

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

                        </Trigger>

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

但仍然不起作用,它说在 ControlTemplate 中找不到触发器属性... 我尝试在触发器属性之后添加它,但也不起作用...


我在 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>"

并且在我的列表框所在的特定 xaml 文件中:

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

但是在执行时,列表框中不再出现任何内容,什么都没有......我不明白......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-09-03 03:05:08

不知道它是否仍然重要(最后一个答案是在 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

谨致问候,

山姆

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文