在列表框中更改所选项目的文本块颜色 [英] Change Textblock Color of Selected Item in Listbox

查看:99
本文介绍了在列表框中更改所选项目的文本块颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚启动了Windows Phone应用程序,并尝试更改列表框中所选项目的(字体)颜色。 (我正在使用Blend 2012)

I have just started a Windows Phone application and am attempting to change the (font) color of the selected item within a listbox. (I am using Blend 2012)

具体来说,我有一个带有DataTemplate的列表框。 DataTemplate在StackPanel中有三个Textblock。

Specifically, I have a listbox with a DataTemplate. The DataTemplate has three Textblocks within a StackPanel.

我的目的是在选择一个项目时,只有StackPanel中第一个(最顶层)Textblock的字体颜色发生变化。

My intention is to have the font color of only the first (topmost) Textblock within the StackPanel change when an item is selected.

当我编辑ControlTemplate / ItemContainerStyle时,我发现我能够通过状态(不透明度或变换等属性)获取整个ContentControl的属性。但是,我无法定位ContentControl
中的任何元素,例如最顶层的Textblock。

When I edit the ControlTemplate / ItemContainerStyle, I find that I am able to get the properties of the entire ContentControl to change via States (properties such as Opacity or Transform). However, I cannot target any elements within the ContentControl such as the topmost Textblock.

是否有一种简单的方法可以使用Blend实现最少的代码隐藏?

Is there a simple way this can be achieved using Blend with minimal code-behind?

推荐答案

我刚刚意识到DataTemplate中的各个元素如果没有被覆盖,将继承ContentControl的属性。

I just realized that the individual elements within the DataTemplate would inherit properties of the ContentControl if it is not overridden.

只需在ControlTemplate中设置ContentControl的Foreground属性,然后确保我的DataTemplate中最顶层的Textblock也没有显式设置Foreground属性似乎可以解决问题。

Simply setting the Foreground Property of the ContentControl in the ControlTemplate, and then making sure that the topmost Textblock within my DataTemplate does not also explicitly set the Foreground Property seems to do the trick.

但是,如果我希望第二个Textblock也改变它的Foreground颜色,但与第一个不同呢?

However, what if I wanted the second Textblock to also change its Foreground color, but differently from the first?

示例:On Select,第一个Textblock的前景变为红色,但第二个Textblock变成蓝色。 (未选中,它们都是白色的)

Example: On Select, the Foreground of the first Textblock turns red, but the second Textblock turns blue. (Unselected, they're all white)


这篇关于在列表框中更改所选项目的文本块颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆