由于颜色原因,列表框中选定的项目无法读取
我在列表框中动态创建堆栈面板的集合。在此堆栈面板中包含水平对齐的标签和复选框。
问题是,当我单击堆栈面板时,选择内容不可读,因为线条变为深蓝色,而字母保持黑色,因此蓝色上的黑色,您什么也看不到。 如何动态更改堆栈面板中所选元素的前景色?我说的是动态的,而不是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道它是否仍然重要(最后一个答案是在 2010 年 3 月 25 日),但对于那些仍然想知道如何实现这一点的人,我这样做了:
在风格部分:
在列表框中,我使用如下所示的 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:
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