WPF中通过DataTemplate更改ListBoxItem的前景色
我面临着一种愚蠢的问题:我无法使用 DataTemplate 更改 ListBoxItem 的前景色。我确信有一个非常简单的解决方案,但我只是没有看到它!
这是我的代码:
<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>
在运行时,颜色是默认的系统颜色。直接通过 ListBox-Attribute 设置前景色不起作用,我希望有人可以提供帮助。
提前致谢!
i'm facing a kindof stupid problem: i'm not able to change the Foreground color of a ListBoxItem with a DataTemplate. 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.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你好,如果我使用你的列表框,它的工作就像你想要的:)为了知道前景色正在工作,我将数据模板更改为文本块中的固定字符串。
您的“绿色”文本块显示任何文本吗?
编辑:我知道这个问题:)
我重写 SystemColors 以获得我想要的。
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.
does your "green" Textblock show any text?
EDIT: i know that problem :)
i override the SystemColors to get what i want.