如何更改列表框中项目的字体颜色(wpf)
问题描述:
我有一个列表框,它被绑定到可观察集合。集合中的元素包含一个名为color的变量。我的列表框的项目已被绑定到集合,但我怎样才能将项目的字体颜色绑定到该集合呢?我已经有与颜色名工作正常更换项目的名称,比如这个如何更改列表框中项目的字体颜色(wpf)
<DataTemplate x:Key="myListBox">
<TextBlock Padding="0,0,10,0"
Text="{Binding Path=Color, Mode=Default}"/>
</DataTemplate>
数据模板,但我似乎无法找到我,以颜色绑定设置其属性。
答
不确定你指的是哪种颜色,但会设置背景和文本/前景色。
<TextBlock Padding="0,0,10,0"
Text="{Binding Path=Color, Mode=Default}"
Background="{Binding myBackgroundColour}"
Foreground="{Binding myTextColour}"
/>
编辑:扶养道具 -
public string Color
{
get { return (string)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
// Using a DependencyProperty as the backing store for Color. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register("Color", typeof(string), typeof(CLASSNAMEHERE), new UIPropertyMetadata("Black"));
更换CLASSNAMEHERE与名字你把它的类,即视图模型类或代码隐藏类的名称。
使用:
this.Color = "Yellow";
答
你可以使用这个资源风格
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Foreground" Value="></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
bla bla bla
</Style>
到底是什么myBackgroundColour/myTextColour? – user579674 2011-05-07 07:59:30
他们是你想要绑定你的颜色的属性。根据您的示例,您可以使用 来设置文本的颜色,并保持背景未设置(当颜色是白色虽然...) –
2011-05-07 08:01:58
我已经这条线前景=“{绑定路径=彩色}”,但它不工作,我不知道为什么。如果我使用前景=“黄色”例如它工作正常。 – user579674 2011-05-07 08:11:22