[英]Change the Forecolor of the ListBoxItem through DataTemplate in WPF
i'm facing a kindof stupid problem: i'm not able to change the Foreground color of a ListBoxItem with a DataTemplate. 我面临一种愚蠢的问题:我无法使用DataTemplate更改ListBoxItem的前景颜色。 I'm sure theres a really simple solution for this but i just don't see it!
我敢肯定有一个非常简单的解决方案,但是我看不到!
Here is my code: 这是我的代码:
<ListBox Height="181" HorizontalAlignment="Left" Margin="12,53,0,0"
Name="lstKeys" VerticalAlignment="Top" Width="491">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="3">
<DockPanel>
<Image Width="32" Height="32" Stretch="Fill" Margin="3"
HorizontalAlignment="Left" Name="image3" VerticalAlignment="Top"
Source="{Binding Icon}" />
<DockPanel Margin="3">
<TextBlock Text="{Binding Product}" Foreground="Green"
DockPanel.Dock="Top" />
<TextBlock Text="{Binding Key}" Foreground="Black"
FontWeight="Bold" DockPanel.Dock="Bottom" />
</DockPanel>
</DockPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
At runtime the color ist the default System Color. 在运行时,颜色为默认的系统颜色。 Setting the Foreground Color directly by ListBox-Attribute doesn't work, i hope somebody can help.
直接通过ListBox-Attribute设置前景颜色不起作用,我希望有人可以提供帮助。
Thanks in advance! 提前致谢!
hi if i use your listbox its work like you want :) To know that the forecolor is working i changed the datatemplate to a fixed string in the textblock. 您好,如果我使用您的列表框,它的工作就像您想要的一样:)要知道前色正在工作,我将datatemplate更改为文本块中的固定字符串。
does your "green" Textblock show any text? 您的“绿色”文本块显示任何文本吗?
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="3">
<DockPanel>
<Image Width="32" Height="32" Stretch="Fill" Margin="3"
HorizontalAlignment="Left" Name="image3" VerticalAlignment="Top"
Source="{Binding Icon}" />
<DockPanel Margin="3">
<TextBlock Text="the binding seems to be wrong" Foreground="Green"
DockPanel.Dock="Top" />
<TextBlock Text="{Binding Key}" Foreground="Black"
FontWeight="Bold" DockPanel.Dock="Bottom" />
</DockPanel>
</DockPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
EDIT: i know that problem :) 编辑:我知道那个问题:)
i override the SystemColors to get what i want. 我重写SystemColors得到我想要的。
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</ListBox.Resources>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.